Skip to content

Commit

Permalink
Update 0459.重复的子字符串.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Amberling1988 committed Apr 8, 2022
1 parent ccfa2c4 commit 559f03c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions problems/0459.重复的子字符串.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 559f03c

Please sign in to comment.