我正在尝试创建一个函数,该函数接受一个字符串作为输入,并返回一个由输入字符串中至少 n
中出现的所有字符组成的字符串。次,按照发生的顺序。
例如,repeat("cchatlpcat", 2)
将返回字符串 cat
.
最佳答案
def repeat(string, n):
result=''
for x in string:
if string.count(x) >= n and x not in result:
result += x
return result
result1 = repeat("cchatlpcat", 2)
result2 = repeat("ccchatalpcatt", 3)
以上两个测试字符串都将返回字符串 cat
。
关于java - 创建返回字符串中重复字符的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54498423/