[英]Python code to find minimum number of characters in a given string
我需要一段代码来查找给定字符串中字母的最少出现次数。
我不能使用 min() function。 我已经有一个 function 来查找字母的最大出现次数。 我需要对代码进行哪些修改?
def getMaxOccuringChar(str):
ASCII_SIZE = 256
count = [0] * ASCII_SIZE
max = -1
c = ''
for i in str:
count[ord(i)] += 1;
for i in str:
if max < count[ord(i)]:
max = count[ord(i)]
c = i
return c
print(getMaxOccuringChar(""))
您可以将max = -1
替换为min = len(str)
并在第二个for
循环中将if max < count[ord(i)]
替换为if min > count[ord(i)]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.