首页 > 试题广场 >

找出字符串中出现最多的字符和个数

[编程题]找出字符串中出现最多的字符和个数
  • 热度指数:626 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
找出字符串中出现最多的字符和个数

输入描述:
一个字符串


输出描述:
输出该字符串中出现最多的字符,以及字符出现的个数。
示例1

输入

'success'

输出

s 3
str = input('')
cnt = {}
for c in str:
    if c not in cnt:
        cnt[c] = 1
    else:
        cnt[c] += 1

for item in sorted(cnt.items(), key = lambda x:x[1], reverse = True):
    print(item[0], item[1])
    break
发表于 2022-10-24 09:23:29 回复(0)