【发布时间】:2015-07-09 22:31:53
【问题描述】:
问题要求我返回一个小写字符串,其中按字母顺序在 s 中出现最频繁的字母。到目前为止,我有:
def mostFrequentLetter(s):
allchar = ''.join(sorted(s))
temp = s.replace(" ","")
max = None
maxchar = None
for alph in allchar.lower():
charcount = temp.count(alph)
if alph not in string.ascii_letters: continue
elif charcount > max:
max = charcount
max = alph
elif charcount == max:
max2 = charcount
max2 = alph
max.append(max2)
return max
如果我输入'aaaabbbb',它应该给我'ab',但它只给我'a'。我该如何解决这个问题?
【问题讨论】:
-
formatyourcodeformatyourcodeformatyourcode
-
对不起第一次使用
-
我不认为我可以使用计数器或设置。没有他们能做到吗?
-
@redbook0301 我添加了一个不使用
Counter的示例。