Easy understand Python solution with explanation. O(N). 65ms beats 80%


  • 0
    R
    class Solution(object):
        def repeatedSubstringPattern(self, str):
            """
            :type str: str
            :rtype: bool
            """
            l = len(str)
            index = l // 2
            # find a possible repeated pattern, if this pattern is repeated throughout the str
            # return True
            
            while index > 0:
                if str[index] == str[0]:
                    # the rest str length match the pattern
                    # check the pattern is repeated
                    if((l - index ) % index == 0):
                        if str.count(str[:index]) ==  l // index:
                            return True
                        
                index -= 1
            
            return False
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.