AcWing 772. 只出现一次的字符
原题链接
中等
作者:
蒟蒻--变革
,
2021-02-01 12:52:37
,
所有人可见
,
阅读 320
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int main()
{
map<char, int> s;//将小写字母作为数组的下标
string a;
cin >> a;
for(auto c : a) s[c]++;
for(auto c : a)//循环查看是否存在出现次数为1的字符
if(s[c] == 1)
{
printf("%c",c);
return 0;
}
printf("no\n");
}
#include <iostream>
using namespace std;
int main()
{
string a;
int cnt[25] = {0};
cin >> a;
for(auto c : a) cnt[(int)(c - 'a')]++;//存储各个字符出现的次数
for(auto c : a)//从头开始遍历字符串,判断是否存在出现次数为1的字符
if(cnt[(int)(c - 'a')] == 1)
{
printf("%c",c);
return 0;
}
printf("no");
}