AcWing 772. 只出现一次的字符
原题链接
中等
作者:
STU756
,
2021-01-20 01:47:13
,
所有人可见
,
阅读 366
//1.Time:O(n^2) Space:O(n)
#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
bool isFind = false;
while(getline(cin, str)) {
for(int i = 0; i < str.size(); i++) {
if(str.find(str[i]) == str.rfind(str[i])) {
cout << str[i] << endl;
isFind = true;
break;
}
}
}
if(!isFind) cout << "no" << endl;
return 0;
}
//2.Time:O(n) Space:O(26)
#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
bool isFind = false;
int count[26] = {0};
while(getline(cin, str)) {
for(int i = 0; i < str.size(); i++) {
++count[str[i] - 'a'];
}
for(int i = 0; i < str.size(); i++) {
if(count[str[i] - 'a'] == 1) {
cout << str[i] << endl;
isFind = true;
break;
}
}
}
if(!isFind) cout << "no" << endl;
return 0;
}