首页 > 试题广场 >

统计字符

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

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

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


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

输入

Have you ever gone shopping and

输出

e
示例2

输入

nowcoder@mail.com

输出

o
# 用了字典
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)
input_str = list(input().strip())
count = {}
for i in input_str:
    if i.isalpha() == False:
        continue
    if i not in count.keys():
        count[i] = 1
    else:
        count[i] += 1
        if count[i] == 3:
            print(i)
            break
发表于 2019-01-04 16:22:38 回复(0)
您的代码已保存
答案错误:您提交的程序没有通过所有的测试用例
case通过率为60.00%

测试用例:
-)%*usBfvO01E#4qa}z^>-xK;*iXBuXLKT%vs7ChGk'%7PQ"pM-)@V*+EPg>?PNu)2C.|3WZlP!PMQ6v}o%]=WT|C6oXjatHG!teXQw[4|5.8gv%$AMM$\*@(\aI9lE6Y[b-&D<$yQJBXd8^uID1fYA*9heF~C:L-rTcw2<w>QqD9<ev6>b\GfeC7,B[3K/d!-T$q:ZmIN#X{,WB`-]*qS*+Mbq}IJ:!<&.t2&FSwSAvy}R]s'z0b|q`iLmeUe,R#oB=}ia8K%>Q.Y,;WWvN0W&v/YWA9d]8I2HMFMKtudo7}f\:^$id2c/=^sR[DPMw;,#|~4T&h37JsK=Zmi7F;WdF3&')c7w_wo0%F11m1x<r><+9a]'f style="background-attachment: scroll; background-clip: border-box; background-color: transparent; background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: repeat; background-size: auto; color: rgb(182, 183, 154);">y\gbV)l(pXaHD3@j[x]m-?z'JB,%)g>gVYJgZj*l*"98]-\xU\k{]D+lL!!.uK1w;C(|t.wF[xT=n~;JfD6EOTa{a*!nyleb;M8k&P50xQM_j]\8]7?3Q'&mYl@0vj^`)f=\i)w}P~.}X3<3wz1YBD\S=E(P3J4O`f}G[R?k]2;]~D*,-Fay/g+A8Ns$"jS!@nv~%Tu~6U{e1:|tl<h7z>RV0n8NM7}W2]Mk=~5KC=mLv3z@\@!Zm@OiJYY}4VPL!%1U0R6t2qI,oRHDaqbbvK]{/|H5@K4vdqUm7j>#Zm_HkTNF-qo]KZ+Z#y'6L!8^Xj&jex_V9>AE~2\T/)^k{bidWy@q5Yo<,y=*P+

对应输出应该为:

P

你的输出为:

%</h7z></r></ev6></w>

逗我玩呢,这个case
发表于 2018-12-09 18:08: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)
import sys
from collections import defaultdict

dict = defaultdict(int)
line = sys.stdin.readline()
for i in line:
    if i.isalpha():
        dict[i] += 1
    if dict[i] == 3:
        print(i)
        break

发表于 2018-09-11 09:45:24 回复(0)
string=''.join(raw_input().split())
my_dict={}
for char in string:
    if char.isalpha():
        try:
            my_dict[char]+=1
            if my_dict[char]==3:
                break
        except:
            my_dict[char]=1
print char
发表于 2018-08-28 00:27:54 回复(0)
temp_str = input()



def find_third(temp_str):
    str1=""
    num=0
    length=len(temp_str)
    for i in range(length):
        if temp_str.count(temp_str[i]) >= 3 and temp_str[i].isalpha():
            num=temp_str.count(temp_str[i],0,i+1)
            if num==3:
                print(temp_str[i])
                break


find_third(temp_str)
失败了好多次才过的,对python字符串处理函数要熟悉才行
发表于 2018-07-13 20:19:18 回复(0)
新手上路!
s = ''
for i in input():
    if i.isalpha():
        s += i
dic = {}
for i in s:
    dic[i] = dic.get(i,0) + 1
    if dic[i] == 3:
        print(i)
        break

发表于 2018-07-08 09:38:26 回复(0)
input_str = input()
input_str = input_str.strip()
characters = {}
for c in input_str:
    if 'a' <= c <= 'z' or 'A' <= c <= 'Z':
        if c in characters:
            if characters[c] == 2:
                print(c)
                break
            else:
                characters[c] = characters[c] + 1
        else:
            characters[c] = 1
发表于 2018-06-15 10:47:27 回复(0)
n = input().replace(' ','')
D = dict()
length = len(n)
for i in range(length):
    if n[i] not in D.keys() and 65<= ord(n[i]) <= 122:
        D[n[i]] = 1
    elif 65<= ord(n[i]) <= 122:
        D[n[i]] += 1
        if D[n[i]] == 3:
            print(n[i])
            break
    else:
        continue
简单暴力解法,从头遍历,如果是英文字符就加入字典,出现次数+1
判断是否到达3次,如果是就输出
发表于 2018-06-04 22:55:03 回复(0)
#字典的get方法很好用
line = input().replace(" ",'')
res = {}
for s in line:
    s = str(s)
    if s.isalpha():
        res[s] = res.get(s,0)+1
        if res[s]>2:
            print(s)
            break

发表于 2018-06-01 14:12:46 回复(0)
from collections import defaultdict

s=input() 
dd = defaultdict(int)
for i in s:
    if i.isalpha():
        dd[i] += 1
        if dd[i] == 3:
            break
print(i)

发表于 2018-05-18 11:41:44 回复(0)
if __name__=='__main__':
    string=input().strip(' ')
    string=list(string)
    times={}
    for i in range(len(string)):
        if string[i].isalpha():
            if times.get(string[i],None)==None:
                times[string[i]]=1
            else:
                times[string[i]]+=1
                if times[string[i]]==3:
                    print(string[i])
                    break

发表于 2018-04-21 12:16:14 回复(0)
创建一个字典统计各个字符出现的次数,这里只统计英文字符,因此用到了isalpha()函数,其他都是很基本的代码,可读性高
s = input().strip()
dic = {}
for ch in s:
    if ch.isalpha():
        if ch not in dic:
            dic[ch] = 1
        else:
            dic[ch] += 1
        if dic[ch] == 3:
            print(ch)
            break

发表于 2018-04-10 06:41:41 回复(0)
#! python3
#-*- ocidng:utf-8 -*-

'''
题目描述
给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符。
输入描述:
输入数据一个字符串,包括字母,数字等。
输出描述:
输出首先出现三次的那个英文字符
示例1
输入
Have you ever gone shopping and
输出
e
'''

import sys

if __name__=='__main__':
    'doc'
    string=input()
    dict1={}
    for i in string:
        if i.isalpha(): 
            dict1.setdefault(i,0)
            dict1[i]+=1
            if dict1[i]>=3:
                print(i)
                break

发表于 2018-03-17 20:03:30 回复(0)
def func():
    num = input()
    d = {}
    for item in num:
        if item.isalpha() is True:
            if item not in d:
                d[item] = 1
            else:
                d[item] += 1
                if d[item] >=3:
                    print(item)
                    break

    
if __name__ == "__main__":
    func()

发表于 2018-03-09 11:24:57 回复(0)
def find(s):
    uper = [0] * 26
    lower = [0] * 26
    for char in s:
        if ord(char) >= ord('a') and ord(char) <= ord('z'):    # is a lower letter
            index = ord(char) - ord('a')
            lower[index] += 1
            if lower[index] ==3:
                return char
        if ord(char) >= ord('A') and ord(char) <= ord('Z'):    # is a upper letter
            index = ord(char) - ord('A')
            uper[index] += 1
            if uper[index] ==3:
                return char
        

if __name__ == "__main__":
    s = input()
    char = find(s)
    print(char)


发表于 2018-03-07 10:57:02 回复(0)
import re
a = [0]*128
def test(s):
    s = re.sub('[^a-zA-Z]','',s)
    for i in range(len(s)):
        a[ord(s[i])] = a[ord(s[i])]+1
        if a[ord(s[i])] == 3:
            print s[i]
            return

s1 = raw_input();
test(s1)
水一波,欢迎各位大佬指点

发表于 2017-12-29 11:26:53 回复(0)