1
Fork 0

Solve implement-strstr.

This commit is contained in:
Bauke 2022-04-06 16:20:49 +02:00
parent 919af81094
commit bb5741755e
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 15 additions and 0 deletions

View File

@ -0,0 +1,14 @@
pub fn str_str(haystack: String, needle: String) -> i32 {
if needle.is_empty() {
return 0;
}
haystack.find(&needle).map(|n| n as i32).unwrap_or(-1)
}
#[test]
fn test_implement_strstr() {
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("aaaaa".to_string(), "".to_string()), 0);
}

View File

@ -1,3 +1,4 @@
pub mod implement_strstr;
pub mod longest_common_prefix;
pub mod palindrome_number;
pub mod reverse_integer;