给定一个英文字符串(包括空格和换行),请找出该字符串中首次出现三次的英文字母(字符需区分大小写) 。如果不存在则输出-1;
输入一个字符串,可包含数字、字母,长度不超过106 个字符
输出第一个出现三次的英文字母,不存在则输出“-1”
i love Kingsoft Office
f
I love KingsoFt Office
-1
#include<iostream> #include<string> using namespace std; int main() { string s;//定义一个字符串变量 getline(cin,s);//从标准输入流中获取字符串 int a[127]={0};//定义一个整型数组来存取字母出现次数 for(int i=0;i<s.length();i++) { a[s[i]]++;//记录字符串中每个字符出现的次数 if(a[s[i]]==3&&isalpha(s[i]))//判断字符出现次数是否为3以及该字符要为字母 { //若是,则输出该字符,并结束程序 cout<<s[i]<<endl; return 0; } } //程序运行到此处,则说明没有出现超高3次的英文字母 cout<<-1<<endl; }
#include<iostream> #include<string> #include<cstring> #include<vector> using namespace std; int main(void){ vector<int> container(127,0); string str; getline(cin,str); for(int pos = 0;pos<str.length();pos++){ if(++container[str[pos]]>=3&&isalpha(str[pos])){ cout<<str[pos]<<endl; return 0; } } cout<<-1<<endl; return 0; }
import sys for line in sys.stdin: char_count={} result_char='-1' for char in line: if char.isalpha(): char_count[char]=char_count.get(char,0)+1 if char_count[char]==3: result_char=char break print(result_char)
#include <iostream> #include <string> #include <map> using namespace std; int main(){ char ch; map<char, int> counter; while (ch=cin.get()) { if (cin.eof()) break; if (isalpha(ch)) { counter[ch]++; if (counter[ch] == 3) { cout << ch; return 0; } } } cout << "-1" << endl; return 0; }
string_content = input() dict_str = {} flag = 0 for str in string_content: if (65 <= ord(str) and ord(str) <= 90)&nbs***bsp;(97 <= ord(str) and ord(str) <= 122): if str not in dict_str: dict_str[str] = 1 else: dict_str[str] += 1 if dict_str[str] == 3: print(str) flag = 1 break if flag == 0: print("-1")