Topic describes
Find the first character that occurs only once in a string (0<= string length <=10000, all letters) and return its position, or -1 (case sensitive) if none exists.
Answer key
#include <iostream>
#include <string>
#include <map>
using namespace std;
int FirstNotRepeatingChar(string str)
{
map<char.int> m;
for (int i = 0; i < str.length(a); i++) m[str[i]]++;for (int i = 0; i < str.length(a); i++) {if (m[str[i]] == 1)
return i;
}
return - 1;
}
int main(a)
{
ios::sync_with_stdio(false);
string s;
cin >> s;
cout << FirstNotRepeatingChar(s) << endl;
return 0;
}
Copy the code