My C++ solution 3ms, with substr, quite easy


  • -1
    X
    class Solution {
    public:
    
        int strStr(string haystack, string needle) {
        
            int hlen = haystack.length();
            int nlen = needle.length();
        
            if(hlen < nlen) return -1;
        
            for(int i = 0;i < hlen-nlen+1; ++i)
                if(haystack.substr(i, nlen) == 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.