implement strStr

class Solution {
public:
    int strStr(string haystack, string needle) {

        if(haystack == needle)
        {
            return 0;
        }

        if(needle.length() > haystack.length())
        {
            return -1;
        }

        for(int i = 0; i<haystack.length()-needle.length()+1; i++)
        {
            int j = 0;
            for(; j < needle.length(); j++)
            {
                if(needle[j] != haystack[i+j])
                {
                    break;
                }
            }

            if(j == needle.length())
            {
                return i;
            }
        }

        return -1;
    }
};

results matching ""

    No results matching ""