-
Notifications
You must be signed in to change notification settings - Fork 0
/
implement_strstr.rs
51 lines (41 loc) · 1.14 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
#[allow(dead_code)]
pub fn str_str(haystack: String, needle: String) -> i32 {
let haystack = haystack.as_bytes();
let needle = needle.as_bytes();
if needle.len() == 0 {
return 0;
}
let mut i = 0;
let mut j = 0;
while i < haystack.len() {
if haystack[i] == needle[j] {
i += 1;
j += 1;
if j == needle.len() {
return (i - j) as i32;
}
} else {
i = i - j + 1;
j = 0;
}
}
-1
}
/*
Algorithm: KMP (Knuth-Morris-Pratt) Algorithm
1. Build a next array for needle string
2. Compare haystack and needle string
Time: O(m + n)
Space: O(n)
m: haystack length
n: needle length
Reference: https://www.youtube.com/watch?v=GTJr8OvyEVQ
*/
#[test]
fn test_str_str() {
assert_eq!(str_str("hello".to_string(), "ll".to_string()), 2);
assert_eq!(str_str("aaaaa".to_string(), "bba".to_string()), -1);
assert_eq!(str_str("".to_string(), "".to_string()), 0);
assert_eq!(str_str("a".to_string(), "".to_string()), 0);
assert_eq!(str_str("".to_string(), "a".to_string()), -1);
}