my 3ms c++ solution


  • 0
    B

    class Solution {
    public:

    bool match(int start,string needle, string word)
    {   int j;
        j=0;
        for(int i=start;i<start+needle.size();i++)
        {   
            if(word[i]!=needle[j])
                return 0;
            else j++;
        }
        return 1;
    }
    
    int strStr(string haystack, string needle) {
        if(needle.size()==0) return 0;
        if(haystack.size()==0) return -1;
        if( needle.size()>haystack.size() ) return -1;
        
        for(int i=0;i<= haystack.size()-needle.size();i++)
            if (match(i,needle,haystack ) ) return i;
        return -1;
        
    }
    

    };


Log in to reply
 

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