a java solution without system's methods


  • 0
    C
    public int strStr(String haystack, String needle) {
        for(int index=0, subIndex=0; index<haystack.length() && subIndex<needle.length(); index++){
            if(haystack.charAt(index) == needle.charAt(subIndex)){
                if(subIndex == needle.length()-1)
                    return index - needle.length() + 1;
                subIndex++;
            }
            else{
            	index -= subIndex;
                subIndex = 0;                
            }
        }
        return needle.length() == 0? 0:-1;
    }

Log in to reply
 

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