28. Implement strStr()
Input: haystack = "hello", needle = "ll"
Output: 2Input: haystack = "aaaaa", needle = "bba"
Output: -1Input: haystack = "", needle = ""
Output: 0Last updated
Input: haystack = "hello", needle = "ll"
Output: 2Input: haystack = "aaaaa", needle = "bba"
Output: -1Input: haystack = "", needle = ""
Output: 0Last updated
public class Solution {
public int StrStr(string haystack, string needle) {
if(haystack != null && needle == ""){
return 0;
}
// if(haystack == needle){
// return 0;
// }
var h_l = haystack.Length;
var n_l = needle.Length;
for(int i = 0; i <= (h_l - n_l); i++){
var j = 0;
while(j < n_l && haystack[i+j] == needle[j]){
j++;
}
if(j == n_l){
return i;
}
}
return -1;
}
}