MCPcopy Create free account
hub / github.com/Ainevsia/Leetcode-Rust / str_str

Method str_str

28. Implement strStr()/src/main.rs:17–28  ·  view source on GitHub ↗
(haystack: String, needle: String)

Source from the content-addressed store, hash-verified

15
16impl Solution {
17 pub fn str_str(haystack: String, needle: String) -> i32 {
18 if needle.len() == 0 { return 0 }
19 if needle.len() > haystack.len() { return -1 }
20 if haystack[..] == needle[..] { return 0 }
21 let win = needle.len();
22 for i in 0..haystack.len() - win + 1 {
23 if needle == haystack[i..i + win] {
24 return i as i32
25 }
26 }
27 -1
28 }
29}
30
31#[cfg(test)]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected