【每日一题1103】寻找子串

大家应该是想复杂了,子串*n = 字符串
这是我的答案 ,已经通过了原地址的提交。顺便搬运2个比较好的解法

def f(words):
    import re
    n = len(words)
    i=1
    max_tuple = (words, 1)
    while(i<=n/2):
        pattern = r'{}'.format(words[:i])
        result = re.findall(pattern, words)
        if(result):
            if len(result)*len(result[0]) == n and len(result)>max_tuple[1]:
                max_tuple = (words[:i], len(result))
        i+=1
    return max_tuple

搬运1:

def find_repeat(s: str) -> tuple:
    for i in range(1, len(s) + 1):
        t, k = s[:i], len(s) // i
        if t * k == s:
            return (t, k)

搬运2:

def f(s):    
     m = __import__('re').match(r'^(.+?)\1*$', s)
     return (m.group(1), len(s)/len(m.group(1)))
    def find_repeat(words: str) -> tuple:
        if len(words) == len(set(words)):
            return (words,1)
        else:
            # 将不重复的转化为列表
            lis = list(words)
            lis1 = []
            lis1.extend(set(words))
            # 获取重复元素
            for i in lis1:
                lis.remove(i)
            len1 = 0   #重复次数
            cha = ""   #重复元素子串
            tup1 = tuple(lis)
            # 获取出现最大的重复次数和第一个次数最多元素
            for i in tup1:
                if tup1.count(i) > len1:
                    len1 = tup1.count(i)
                    cha = i
            #判断是否有组合情况
            for i in tup1:
                if tup1.count(i) == len1:
                    if i not in cha:
                        cha = cha+i
            return (cha,len1+1)
    assert find_repeat("abababa") == ("a", 4)
    assert find_repeat("abcde") == ("abcde", 1)

会不会有一种可能:
字符串为:abefababc
这时的结果为:(“ab”,“ef”,2)