T1104 Find the first character that appears only once

T1104 Find the first character that only appears once

Given a string containing only lowercase letters, find the first character that appears only once. If not, output "no".

Input format

A character string with a length less than 100000100000.

Output format

Output the first character that appears only once, or output "no" if there is none. (The extra space at the end of each line during output does not affect the correctness of the answer)

Sample input

abcabd

Sample output

c

Code

#include <iostream>
#include <string>
using namespace std;

int main(){
    
    
	string s;
	int count[26] = {
    
    0};//使用一个数组来存储26个字母出现的次数 
	int len = 0;//存储s的长度 
	getline(cin,s);
	len = s.length();
	
	// 首先循环一次s,统计所有字母出现的次数
	for(int i=0; i<len; i++){
    
    
		count[s[i]-'a']++;//'a'的ASCII码为97 
	} 
	//再循环遍历统计结果,出现第一个出现一次的字符就输出,并终止
	for(int i=0; i<len; i++){
    
    
		if(count[s[i]-'a'] == 1){
    
    
			cout << s[i];
			return 0;
		}
	} 
	//如果没有则输出no
	cout << "no"; 
	return 0;
} 

Guess you like

Origin blog.csdn.net/qq_44524918/article/details/108630009