-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimplement-strstr.rs
87 lines (82 loc) · 2.27 KB
/
implement-strstr.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#![allow(dead_code, unused, unused_variables)]
fn main() {
println!(
"{:?}",
Solution::prefix_table("aabaabaaa".to_string().as_bytes())
);
println!(
"{:?}",
Solution::prefix_table("abcdabca".to_string().as_bytes())
);
println!(
"{:?}",
Solution::prefix_table("ababcaabc".to_string().as_bytes())
);
println!(
"{:?}",
Solution::prefix_table("aabaaac".to_string().as_bytes())
);
println!(
"{}",
Solution::str_str("abxabcabcaby".to_string(), "abcaby".to_string())
);
println!(
"{}",
Solution::str_str("ababcaababcaabc".to_string(), "ababcaabc".to_string())
);
println!(
"{}",
Solution::str_str("aabaaabaaac".to_string(), "aabaaac".to_string())
);
}
struct Solution;
impl Solution {
pub fn str_str(haystack: String, needle: String) -> i32 {
if needle.len() == 0 {
return 0;
}
let (nums, s) = (needle.as_bytes(), haystack.as_bytes());
let next = Self::prefix_table(&nums[..]);
let (mut i, mut j) = (0, 0); // i是s的下标,j是next的下标
while i < s.len() {
if s[i] == nums[j] {
if j == nums.len() - 1 {
return (i - j) as i32;
}
i += 1;
j += 1;
} else {
if j != 0 {
j = next[j - 1];
} else {
i += 1;
}
}
}
-1
}
/// 获取到前缀表
fn prefix_table(nums: &[u8]) -> Vec<usize> {
let mut tables = vec![0usize; nums.len()];
tables[0] = 0; // 第一个元素的前缀表为0
let mut j = 0;
for i in 1..nums.len() {
if nums[j] == nums[i] {
tables[i] = tables[i - 1] + 1;
j += 1;
} else {
while j > 0 {
j = tables[j - 1];
if nums[j] == nums[i] {
tables[i] = tables[j] + 1;
j += 1;
break;
} else {
tables[i] = 0;
}
}
}
}
tables
}
}