Leetcode: Implement strStr()

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

public String strStr(String haystack, String needle) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(needle==null){
            return haystack;
        }

        int start = 0;
        int index1 = 0;
        int index2 = 0; 
        while(index1<haystack.length() && index2<needle.length()){
            if(haystack.charAt(index1)==needle.charAt(index2)){
                index1++;
                index2++;                
            }else{
                start++;
                index1=start;
                index2=0;

            }
        }
        if(index2==needle.length() && index1-start==needle.length()){
            return haystack.substring(start);
        }else{
            return null;
        }
    }
FacebookTwitterGoogle+Share

Leave a Reply

Your email address will not be published. Required fields are marked *