diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index ccfb485c..6a9b4260 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -421,5 +421,48 @@ function repeatedSubstringPattern(s: string): boolean { }; ``` + +Swift: + +> 前缀表统一减一 +```swift + func repeatedSubstringPattern(_ s: String) -> Bool { + + let sArr = Array(s) + let len = s.count + if len == 0 { + return false + } + var next = Array.init(repeating: -1, count: len) + + getNext(&next,sArr) + + if next.last != -1 && len % (len - (next[len-1] + 1)) == 0{ + return true + } + + return false + } + + func getNext(_ next: inout [Int], _ str:[Character]) { + + var j = -1 + next[0] = j + + for i in 1 ..< str.count { + + while j >= 0 && str[j+1] != str[i] { + j = next[j] + } + + if str[i] == str[j+1] { + j += 1 + } + + next[i] = j + } + } +``` + -----------------------