From 559f03c92869ca55ae929d66de872f5ddda70567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=9F=E4=BB=A4=E4=BB=A4?= Date: Fri, 8 Apr 2022 18:11:05 +0800 Subject: [PATCH] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) 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 + } + } +``` + -----------------------