Clean and Optimized brute force (c++, 6ms)


  • 0
    S

    ...
    int strStr(string haystack, string needle) {

        int h=haystack.size();
        int n=needle.size();
        int i=0;
        
        while(h-i>=n){
            int l=0;
            for(int j=0;j<needle.size();j++){
                if(needle[j]==haystack[i+j])
                 l++;
                 
            }
            
            if(l==needle.size())
            return i;
            
            i++;
        }
        
        return -1;
    }
    

    ...


Log in to reply
 

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