繁体   English   中英

Python 代码查找给定字符串中的最小字符数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM