Clear and clean solution


  • 0
    X
    bool match(string s,string pat)
        {
            int pos1=0,pos2=0,len=0;
            while(pos1<s.size()&&pos2<pat.size())
            {
                if(s[pos1]==pat[pos2])
                    pos1++,pos2++,len++;
                else
                    pos1++;
            }
            return len==pat.size();
        }
        
        string findLongestWord(string s, vector<string>& d) 
        {
            string res;
            for(string word:d)
                if(match(s,word)&&(word.size()>res.size()||(word.size()==res.size()&&word<res)))
                    res=word;
            return  res;
        }
    

Log in to reply
 

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