update 0459.重复的子字符串.md about rust 前缀表统一 -1

This commit is contained in:
fw_qaq
2022-10-25 23:26:04 +08:00
committed by GitHub
parent 563cd82551
commit 856907e3d5

View File

@ -615,6 +615,35 @@ impl Solution {
} }
``` ```
>前缀表统一减一
```rust
pub fn get_next(next_len: usize, s: &Vec<char>) -> Vec<i32> {
let mut next = vec![-1; next_len];
let mut j = -1;
for i in 1..s.len() {
while j >= 0 && s[i] != s[(j + 1) as usize] {
j = next[j as usize];
}
if s[i] == s[(j + 1) as usize] {
j += 1;
}
next[i] = j;
}
next
}
pub fn repeated_substring_pattern(s: String) -> bool {
let s_chars = s.chars().collect::<Vec<char>>();
let next = Self::get_next(s_chars.len(), &s_chars);
if next[s_chars.len() - 1] >= 0
&& s_chars.len() % (s_chars.len() - (next[s_chars.len() - 1] + 1) as usize) == 0
{
return true;
}
false
}
```
<p align="center"> <p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">