def find_repeat(word:str):
length=len(word)
for i in range(len(word)):
if length%(i+1)==0:
res2=length/(i+1)
res1=word[0:(i+1)]
if res1*int(res2)==word:
return (res1,int(res2))
else:
continue
else:
continue
def find_repeat(words: str) -> tuple:
son_list = [words[i:i + x + 1] for x in range(len(words)) for i in range(len(words) - x)]
count_dict = {}
for w in son_list:
if w not in count_dict.keys():
count_dict[w] = words.count(w)
else:
if words.count(w) > count_dict[w]:
count_dict[w] = words.count(w)
count_max = 0
max_word = ''
for key, value in count_dict.items():
if count_dict[key] > count_max:
count_max = count_dict[key]
max_word = key
if count_dict[key] == count_max:
if len(key) > len(max_word):
max_word = key
return max_word, count_max
assert find_repeat("ababab") == ("ab", 3)
assert find_repeat("abcde") == ("abcde", 1)
def find_repeat(words):
lengh=len(words)
min_len=float("inf")
max_count=0
for i in range(lengh):
for j in range(i+2,lengh):
if len(words[i:j])<min_len and words.count(words[i:j])>max_count:
min_len=len(words[i:j])
max_count=words.count(words[i:j])
word=words[i:j]
if max_count>1:
return (word,max_count)
if max_count==1:
return (words,max_count)
assert find_repeat("ababab") == ("ab", 3)
assert find_repeat("abcde") == ("abcde", 1)
def find_repeat(self, word:str):
'''
给定一个非空字符串words,只包含纯小写字母。请编写一个函数,找出其中最短的一个子字符串,以及它重复的最大次数。
示例:
输入: ababab,输出: ("ab", 3)。
输入:abcde,输出:("abcde", 1)。
:return:
'''
repeat_list = []
wordlen = len(word)
for i in range(0, wordlen):
for j in range(i+1, wordlen):
for k in range(2, wordlen - j + 1):
if i + k - 1 >= j:
break
if word[j: j+k] == word[i: i+k]:
repeat_list.append(word[i: i + k])
continue
else:
continue
pass
repeat_set = set(repeat_list)
repeat_dict = {}
result = tuple((word, 1))
max_num = 1
for data in repeat_set:
count = 1
for data2 in repeat_list:
if data == data2:
count +=1
repeat_dict[data] = count
if count > max_num:
result = (data, count)
print(repeat_list)
print(repeat_dict)
return result
def find_repeat(words: str) -> tuple:
dic1={}
list2=[]
for i in range(len(words)):
for j in range(i+2,len(words)+1):
dic1[words[i:j]]=words.count(words[i:j])
list1=sorted(dic1.items(),key=lambda xy:xy[1],reverse=True)
max_num=max(list1,key=lambda x:x[1])[1]
for j in list1:
if j[1]==max_num:
list2.append(j)
if len(list2)>1:
return max(list2,key=lambda x:len(x[0]))
else:
return list2[0]
assert find_repeat("ababab") == ("ab", 3)
assert find_repeat("abcde") == ("abcde", 1)