Sharing my C++ solution


  • 0
    T
    class Solution {
    public:
        int strStr(string haystack, string needle) {
            int nH = haystack.length();
            int nN = needle.length();
            if(nN==0)
                return 0;
            if(nN>nH)
                return -1;
            for(int i=0; i<=nH-nN; i++)
                if(haystack.substr(i, nN)==needle)
                    return i;
                    
            return -1;
        }
    };

Log in to reply
 

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