-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0028_implement_strstr.rs
More file actions
44 lines (40 loc) · 1.19 KB
/
s0028_implement_strstr.rs
File metadata and controls
44 lines (40 loc) · 1.19 KB
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
#![allow(unused)]
pub struct Solution {}
impl Solution {
// O(nm) O(1)
pub fn str_str(haystack: String, needle: String) -> i32 {
// What should we return when needle is an empty string? This is a great question
// to ask during an interview.
//
//For the purpose of this problem, we will return 0 when needle is an empty string.
//This is consistent to C's strstr() and Java's indexOf().
if haystack.len() == 0 && needle.len() != 0 {
return -1;
}
if haystack.len() == 0 || needle.len() == 0 {
return 0;
}
let needle = needle.as_bytes();
match haystack
.as_bytes()
.windows(needle.len())
.position(|slice| slice == needle)
{
Some(x) => x as i32,
_ => -1i32,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_28() {
assert_eq!(Solution::str_str("hello".to_string(), "ll".to_string(),), 2);
assert_eq!(
Solution::str_str("aaaaa".to_string(), "bba".to_string(),),
-1
);
assert_eq!(Solution::str_str("".to_string(), "".to_string(),), 0);
}
}