28. Implement strStr()

28. Implement strStr()

class Solution {
    public int strStr(String haystack, String needle) {
        if (needle.equals("")) return 0;
        if (haystack.equals("")) return -1;
        for (int i = 0; i < haystack.length();){
            int temp = i;
            int neadleIndex= 0;
            while(i < haystack.length() && 
                  neadleIndex < needle.length() &&
                  haystack.charAt(i) ==needle.charAt(neadleIndex)){
                neadleIndex++;
                i++;
                }
                if(neadleIndex-1 == needle.length() -1) return temp;
            else {
                i = temp+1 ;
            }
            }
        
        return -1;
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s