牛客网 NC31 第一个只出现一次的字符
class Solution {
public:
int FirstNotRepeatingChar(string str) {
int upperLetters[26] = { 0 };
int lowerLetters[26] = { 0 };
for (char& ch : str)
{
int ret = 0;
if (ch >= 'a' && ch <= 'z')
{
ret = ch - 'a';
++lowerLetters[ret];
}
else
{
ret = ch - 'A';
++upperLetters[ret];
}
}
auto it = str.begin();
while (it != str.end())
{
if (upperLetters[*it - 'A'] == 1 || 1 == lowerLetters[*it - 'a'])
return it - str.begin();
it++;
}
return -1;
}
};