Java one pass 5 line solution (Easy to understand)


  • 0
    E
    public int strStr(String haystack, String needle) {
        if(needle.equals("")) return 0;
        int L = needle.length();
        for(int i=0; i<=haystack.length()-L; i++) 
            if(haystack.substring(i,i+L).equals(needle)) return i;
        return -1; // if index not found, return -1
    }

Log in to reply
 

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