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