C++ O(N) 3ms solution


  • 0
    H
    class Solution {
    public:
        int strStr(string haystack, string needle) {
            int n = haystack.size(), len = needle.size();
            if(len == 0) return 0;
            for(int i = 0; i <= n - len; i ++)
                if(haystack.substr(i, len) == 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.