给定一个只包含小写字母的字符串,请你找到第一个仅出现一次的字符。如果没有,输出no。 C++
时间: 2023-08-14 22:12:15 浏览: 39
可以使用哈希表来解决这个问题。对于每个字符,我们可以记录它出现的次数,然后再遍历一遍字符串,找到第一个出现次数为 1 的字符即可。
具体实现如下:
```cpp
#include <iostream>
#include <unordered_map>
using namespace std;
char findFirstUniqueChar(string s) {
unordered_map<char, int> count;
for (char c : s) {
count[c]++;
}
for (char c : s) {
if (count[c] == 1) {
return c;
}
}
return ' ';
}
int main() {
string s = "abaccdeff";
char res = findFirstUniqueChar(s);
if (res == ' ') {
cout << "no" << endl;
} else {
cout << res << endl;
}
return 0;
}
```
输出结果为:
```
b
```