首页 > 试题广场 >

统计字符

[编程题]统计字符
  • 热度指数:43642 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定一个字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符(需要区分大小写)。

数据范围:字符串长度满足

输入描述:
输入数据一个字符串,包括字母,数字,字符等。


输出描述:
输出首先出现三次的那个英文字符
示例1

输入

Have you ever gone shopping and

输出

e
示例2

输入

nowcoder@mail.com

输出

o
//用map<char,int> 存储数据,对每一个读取的数据进行判断
#include<iostream>
#include <map>
using namespace std;

int main()
{
	map<char, int> letter;
	char l;
	while (cin >>l)
	{
    	if((l>='a'&&l<='z')||l>='A'&&l<='Z')
       	{
            ++letter[l];
			if (letter[l] == 3)
			{
			cout << l;
            break;
			}
        }
		
	}

	system("pause");
	return 0;
}


发表于 2017-08-29 15:51:46 回复(1)
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str;
	getline(cin, str);
	int a[1000] = { 0 };
	for (int i = 0;i<str.size();++i)
    {
        if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')            
		    ++a[str[i]];
        if(a[str[i]]==3)
        {
            printf("%c\n",str[i]);
            break;
        }
    }
	return 0;
}

发表于 2017-09-04 10:36:06 回复(5)
str = ''.join(input().split())
for i in range(len(str)):
    s = str[i]
    if s.isalpha():
        num = str[0:i+1].count(s)
        if num==3:
            print(s)
            break

发表于 2017-08-17 22:34:08 回复(0)

用ASCII码哈希 O(n)

#include <iostream>
#include <string>
using namespace std;
int ha[256];
int main(){
    string s;
    while(cin>>s){
        for(int i = 0; i < s.length(); i++){
            if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){
                ha[int(s[i])]++;
                if(ha[int(s[i])] == 3){printf("%c\n", s[i]); return 0;}
            }
        }
    }
    return 0;
}
发表于 2018-09-29 17:31:25 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            char[] arr = sc.nextLine().replaceAll("[^a-zA-Z]", "").toCharArray();
            Map<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < arr.length; i ++) {
                map.put(arr[i], map.containsKey(arr[i]) ? map.get(arr[i]) + 1 : 1);
                if(map.get(arr[i]) == 3) {
                    System.out.println(arr[i]);
                    break;
                }
            }
        }
    }
}

发表于 2017-11-20 23:29:43 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        StringBuilder str = new StringBuilder();
        str.append(sc.nextLine());
        char[] c = str.toString().trim().toCharArray();
        int[] a = new int[127];
        for(char s : c){
            a[s]++;
            if((s >= 'a' && s <= 'z' || s >= 'A' && s <= 'Z') && a[s] == 3){
                System.out.print(s);
                break;
            }
        }
    }
}

发表于 2017-08-11 22:19:27 回复(9)

好像看了下没用正则的?

import re

print(re.match('.*([a-zA-Z]).*\\1.*\\1.*', input()[::-1]).group(1))
发表于 2018-10-18 15:13:02 回复(0)
#include<stdio.h>
#include<string.h>
char s[1000000];
int book[500],i,res;
int main(){
    while(gets(s)){
        memset(book,0,sizeof(book));
        for(i=0;s[i]!='\0';i++)
            if(('0'<=s[i]&&s[i]<='9'||
               ('a'<=s[i]&&s[i]<='z'||'A'<=s[i]&&s[i]<='Z'))
               &&++book[s[i]]==3){
                printf("%c\n",s[i]);
                break;
            }
    }
}

发表于 2017-09-22 14:16:25 回复(0)
#include<iostream>
#include<string>
#include<map>
using namespace std;

int main()
{
	string s;
	while(getline(cin,s))
       {	
            map<char, int> m;
            for (int i = 0; i < s.length(); i++)
               {
                   m[s[i]]++;
                   //if(((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'))&&m[s[i]]>=3)
                   //tolower(),大写转小写,同toupper(),小写转大写
                   if(tolower(s[i])>='a'&&tolower(s[i])<='z'&&m[s[i]]>=3)
                     {
                        cout<<s[i]<<endl;
                        break;
                     }
               }
            /*map输出的一般用法举例
            map<char, int>::iterator iter;
            for (iter = m.begin(); iter !=m.end(); iter++)
              {
                if (((iter->first>='a'&&iter->first<='z')||(iter->first>='A'&&iter->first<='Z'))&&iter->second>=3)
                 {
                    cout <<iter->first<< endl;
                    break;
		         }
              }
            */
       }
	return 0;
}

发表于 2017-09-06 15:12:26 回复(0)

注意,是字母,其他的不予以统计,坑逼题。调试了半天,怀疑人生

<?php

$str = trim(fgets(STDIN));

$arr = [];

for($i=0; $i<strlen($str); $i++){
    if($str[$i]>='A' && $str[$i]<='z'){
         if(array_key_exists($str[$i], $arr)){
        $arr[$str[$i]]++; 
            if($arr[$str[$i]] == 3){
                echo $str[$i];
                break;
            }
        }else{
            $arr[$str[$i]]=1;
        }   
    }
}
发表于 2017-09-15 09:54:19 回复(1)
对遍历字符串中的字符,并对其进行计数,碰到计数为3的字符就立即返回
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String sentence;
        while((sentence = br.readLine()) != null)
            solve(sentence.toCharArray());
    }
    
    private static char solve(char[] sentence) {
        int[] counter = new int[128];
        for(int i = 0; i < sentence.length; i++){
            if((sentence[i] >= 'a' && sentence[i] <= 'z') || 
               (sentence[i] >= 'A' && sentence[i] <= 'Z')){
                counter[sentence[i]] ++;
                if(counter[sentence[i]] == 3){
                    System.out.println(sentence[i]);
                    break;
                }
            }
        }
        return ' ';
    }
}

发表于 2021-04-10 21:25:48 回复(0)
# 用了字典
s = list(input())
slist = set(s)
dic = {}
for item in set(s):
    dic[item] = 0
for item in s:
    if item.isalpha():
        dic[item] += 1
        if dic[item] == 3:
            print(item)
            break
else:
    print('null')

发表于 2019-08-01 10:54:45 回复(0)
结果必须是英文字符
发表于 2018-12-24 09:55:17 回复(0)
import java.io.BufferedInputStream;
import java.io.IOException;

public class Main {

    public static void getChar(String s) {
        char[] c = s.toCharArray();
        for (int i = 2; i < s.length(); i++) {
            if (c[i] < 65 || c[i] > 122) {
                continue;
            }
            String sb = s.substring(0, i);
            int l1 = sb.indexOf(c[i]);
            if (l1 >= 0) {
                int l2 = sb.lastIndexOf(c[i]);
                if (l1 != l2) {
                    System.out.println(c[i]);
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        BufferedInputStream bis = new BufferedInputStream(System.in);
        byte[] b = new byte[1024];
        try {
            bis.read(b);
            String str = new String(b);
            getChar(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
发表于 2018-11-01 09:18:00 回复(0)
import java.util.Scanner;
import java.util.Map;
import java.util.TreeMap;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            char[] ch = sc.nextLine().toCharArray();
            Map<Character,Integer> map = new TreeMap<>();
            for(int i=0;i<ch.length;i++){
                if((ch[i] >='a' && ch[i]<='z') || (ch[i] >='A' && ch[i] <='Z')){
                    if(map.containsKey(ch[i])){
                        map.put(ch[i],map.get(ch[i])+1);
                        if(map.get(ch[i]) == 3){
                            System.out.println(ch[i]);
                            break;
                        }
                    }else{
                        map.put(ch[i],1);
                    }
                }
            }
        }
    }
}

发表于 2018-10-06 14:19:18 回复(0)
import sys

def func(ch):
    d = {}
    for i in ch:
        if i.isalpha():
            d[i] = d.get(i,0)+1
            if d[i] == 3:
                return i
    return False

if __name__ =='__main__':
    ch = ''.join(sys.stdin.readline().strip().split())
    s = func(ch)
    if s:
        print(s)

发表于 2018-09-23 18:46:33 回复(0)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
    char strput[53];
    memset(strput, 0, sizeof(strput));
    int c;
    while ((c = getchar()) != EOF) {
        if (isalpha(c)) {
            if (islower(c)) {
                strput[c - 71]++;
                if (strput[c - 71] == 3) {
                    printf("%c", c);
                    return 0;
                }
            }
            else {
                strput[c - 65]++;
                if (strput[c - 65] == 3) {
                    printf("%c", c);
                    return 0;
                }
            }
        }
    }
}

发表于 2018-06-22 10:52:48 回复(0)

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
            String[] str = new String[s.length()];
            //将得到的字符串转换成字符串数组
            for (int i = 0; i < s.length(); i++) {
                str[i] = String.valueOf(s.charAt(i));
            }
            //创建一个map集合来存放取出的字符串
            Map<String, Integer> map = new HashMap<String, Integer>();
            //第一次出现三次的字符
            String str1 = "";
            //从字符串中取值放到map中
            for (int i = 0; i < str.length; i++) {
                if(map.containsKey(str[i]) && Character.isLetter(str[i].charAt(0))){
                    Integer value = map.get(str[i]);
                    if(value == 2){
                        value += 1;
                        map.put(str[i], value);
                        str1 = str[i];
                        break;
                    }else{
                        value += 1;
                        map.put(str[i], value);
                    }
                    
                }else{
                    map.put(str[i], 1);
                }
            }
            System.out.println(str1);
            
        }
    }
}
 
发表于 2018-04-20 12:02:50 回复(0)
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
int main() {
    unordered_map<char, int> dir;
    string s;
    getline(cin, s);
    for (int i = 0, m = s.size(); i < m; i++) {
        if (isalpha(s[i]) && ++dir[s[i]] == 3) {
            cout << s[i] << endl;
            break;
        }
    }
    return 0;
}

发表于 2018-04-19 19:59:17 回复(0)
/*c语言版*/
#include <stdio.h>

int a[128] = {0};

int hash(char ch)
{
    if( (ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
    {
        a[(int)ch]++;
        if(a[(int)ch] == 3)
        {
            printf("%c",ch);
            return 3;
        }
    }
    return 0;
}

int main(void)
{
    char c;
    int ret = 0;
    while(scanf("%c",&c) != '\n')
    {
        ret = hash(c);
        if(ret == 3)
        {
            return 0;
        }
    }
    
    return 0;
}
发表于 2018-04-13 15:44:04 回复(0)