Skip to content

Commit

Permalink
Update 0459.重复的子字符串.md 添加swift方法(前缀表统一不减一)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amberling1988 committed Apr 13, 2022
1 parent 33be177 commit 0fc1cb3
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions problems/0459.重复的子字符串.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,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
}
}

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

0 comments on commit 0fc1cb3

Please sign in to comment.