Update 0459.重复的子字符串.md

This commit is contained in:
孟令令
2022-04-08 18:11:05 +08:00
parent ccfa2c495f
commit 559f03c928

View File

@ -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
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>