华为机试2-计算字符个数

题目描述
写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。

输入描述:
第一行输入一个有字母和数字以及空格组成的字符串,第二行输入一个字符。

输出描述:
输出输入字符串中含有该字符的个数。

示例1
输入
ABCDEF
A
输出
1

 

参考1:

注意,不区分大小写,字符串

from collections import Counter
num = list(input().split())
n = input()
res = ''
for s in num:
    res = res + s
res_l = res.lower()     #不区分大小写,统一转化为小写
n_l = n.lower()
m = Counter(res_l)    #Counter返回字典形式
print(m[n_l])

执行结果: 答案正确:恭喜!您提交的程序通过了所有的测试用例 用例通过率: 100.00% 运行时间: 22ms 占用内存:3704KB

 

参考2:

num = input().lower()
s = input().lower()
n = num.count(s)    #字符串操作
print(n)

执行结果: 答案正确:恭喜!您提交的程序通过了所有的测试用例 用例通过率: 100.00% 运行时间: 22ms 占用内存:3320KB

posted @ 2020-08-18 14:27  Andy_George  阅读(140)  评论(0编辑  收藏  举报