How to find repeating sequence of characters in a given array?

后端 未结 14 836
故里飘歌
故里飘歌 2020-12-02 12:42

My problem is to find the repeating sequence of characters in the given array. simply, to identify the pattern in which the characters are appearing.

          


        
14条回答
  •  甜味超标
    2020-12-02 13:01

    In Python, you can leverage regexes thus:

    def recurrence(text):
        import re
        for i in range(1, len(text)/2 + 1):
            m = re.match(r'^(.{%d})\1+$'%i, text)
            if m: return m.group(1)
    
    recurrence('abcabc') # Returns 'abc'
    

    I'm not sure how this would translate to Java or C. (That's one of the reasons I like Python, I guess. :-)

提交回复
热议问题