Shortest Repeating Sub-String

前端 未结 2 887
猫巷女王i
猫巷女王i 2021-01-18 12:25

I am looking for an efficient way to extract the shortest repeating substring. For example:

input1 = \'dabcdbcdbcdd\'
ouput1 = \'bcd\'

input2 = \'cbabababac         


        
2条回答
  •  孤城傲影
    2021-01-18 13:05

    ^ matches at the start of a string. In your example the repeating substrings don't start at the beginning. Similar for $. Without ^ and $ the pattern .*? always matches empty string. Demo:

    import re
    
    def srp(s):
        return re.search(r'(.+?)\1+', s).group(1)
    
    print srp('dabcdbcdbcdd') # -> bcd
    print srp('cbabababac')   # -> ba
    

    Though It doesn't find the shortest substring.

提交回复
热议问题