Very short Accepted Java solution, easy to understand!

  • 0
    public class Solution {
        public int strStr(String haystack, String needle) {
            if(haystack == null || needle == null || needle.length()>haystack.length()) return -1;
            if(needle.length()==0) return 0;
            int i=0,j=0,k=0;
            for(i=0; i<haystack.length()-needle.length()+1; ++i){
                j=i; k=0;
                    if(k==needle.length()-1) return i;
                    j++; k++;
            return -1;

Log in to reply

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