Find the repeating substring a string is composed of, if it exists

后端 未结 2 785
一个人的身影
一个人的身影 2021-01-25 06:53

How would you go about splitting a normal string in to as many identical pieces as possible whilst using all characters. For example

a = \"abab\"
相关标签:
2条回答
  • 2021-01-25 07:18

    This is very similar, but not identical, to How can I tell if a string repeats itself in Python? – the difference being that that question only asks to determine whether a string is made up of identical repeating substrings, rather than what the repeating substring (if any) is.

    The accepted (and by far the best performing) answer to that question can be adapted to return the repeating string if there is one:

    def repeater(s):
        i = (s+s)[1:-1].find(s)
        if i == -1:
            return s
        else:
            return s[:i+1]
    

    Examples:

    >>> repeater('abab')
    'ab'
    >>> repeater('ababc')
    'ababc'
    >>> repeater('xyz' * 1000000)
    'xyz'
    >>> repeater('xyz' * 50 + 'q')
    'xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzq'
    
    0 讨论(0)
  • 2021-01-25 07:19

    It seems that repeating substring has no pre and after letters, so it also could be this way:

    In[4]: re.sub(r'^([a-z]+)\1$',r'\1','abab')
    Out[4]: 'ab'
    In[5]: re.sub(r'^([a-z]+)\1$',r'\1','ababc')
    Out[5]: 'ababc' 
    

    ([a-z]+) means substring, \1 means repeat.

    EDIT :

    re.sub(r'^([a-z]+)\1{1,}$',r'\1','abcabcabcabc')
    'abc'
    
    0 讨论(0)
提交回复
热议问题