找出字符串中第一个只出现一次的字符
描述
找出字符串中第一个只出现一次的字符
数据范围:输入的字符串长度满足
输入描述:
输入几个非空字符串
输出描述:
输出第一个只出现一次的字符,如果不存在输出-1
示例1
输入:
asdfasdfo
aabb
输出:
o
-1
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
while(getline(cin, str)){
int chac[128]={0};
for(int i=0;i<str.size();++i){
chac[str[i]-'a']++;
}
bool flag=false;
for(int i=0;i<str.size();++i){
if(chac[str[i]-'a']==1){
cout<<str[i]<<endl;
flag=true;
break;
}
}
if(!flag){
cout<<-1<<endl;
}
}
return 0;
}