Not so elegent, but easy to understand java solution


  • -1
    E
    public class Solution {
          public int strStr(String haystack, String needle) {
            int len = needle.length();
            int len1 = haystack.length();
            if(len == 0) return 0;
            if(len1<len) return -1;
            for(int i = 0; i<len1-len+1; i++){
                if(haystack.substring(i,i+len).equals(needle)){
                    return i;
                }
            }
        
        return -1;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.