Golang & O(n)


  • 0
    func repeatedSubstringPattern(s string) bool {
        prefix := Next(s)
        maxSubString := prefix[len(prefix)-1] + 1
        return maxSubString > 0 && len(s)%(len(s)-maxSubString) == 0
    }
    
    func Next(s string) []int {
        next := make([]int, len(s))
        k := -1
        next[0] = k
        for i := 1; i < len(s); i++ {
            for k > -1 && s[k+1] != s[i] {
                k = next[k]   
            }
            if s[k+1] == s[i] {
                k += 1
            }
            next[i] = k
        }
        return next
    }
    

Log in to reply
 

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