Simple java solution


  • 0
    Y
        if(haystack.equals(needle)) return 0;
        for(int i = 0; i < haystack.length() - needle.length() + 1; i++){
            if(needle.equals(haystack.substring(i, i + needle.length()))) return i;
        }
        return -1;

Log in to reply
 

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