c++ solution


  • 0
    W
    bool complen(const string& a,const string& b) {
    	if (a.size() != b.size()) return a.size()>b.size();
    	return a < b;
    }
    class Solution {
    public:
        string findLongestWord(string s, vector<string>& d) {
            sort(d.begin(),d.end(),complen);
    		//printVec(d);
    		for (auto i=0; i<d.size(); ++i) {
    			const string& target = d[i];
    			size_t pos=0;
    			size_t j=0;
    			for (j=0; j<target.size(); ++j) {
    				auto idx = s.find(target[j],pos);
    				if (idx==string::npos) break;
    				pos=idx+1;
    			}
    			if (j==target.size()) return target;
    		}
    		return "";
        }
    };
    
    

Log in to reply
 

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