Easy to understand C++ Solution


  • 0
    A

    Just sharing my solution here:

    class String
    {
    public:
    	string str;
    	int s;
    	String(string& tmp)
    	{
    		str = tmp;
    		s=str.size();
    	}
    	//
    	bool operator <(const String& rhs) const
    	{
    		return s < rhs.s;
    	}
    	//
    	bool operator >(const String& rhs) const
    	{
    		return s > rhs.s;
    	}
    	//
    	bool canYou(string s)
    	{
    		int i=0, j=0;
    		while(i<s.size() && j<str.size())
    		{
    			if(s[i] == str[j])
    			{
    				i++; j++;
    			} else 
    				i++;
    		}
    		return j == str.size();
    	}
    };
    string findLongestWord(string s, vector<string>& d) 
    {
    	string result; 
    	vector<String> heap;
    	//sort(s.begin(), s.end());
    	for(int i=0; i<d.size(); i++)
    	{
    		heap.push_back(String(d[i]));
    	}
    	std::make_heap (heap.begin(),heap.end());
    	//
    	for(int i=0; i<d.size(); i++)
    	{
    		if(heap.front().canYou(s)) 
    		{
    			string e = heap.front().str;
    			if(result.empty())
    				result = e;
    			else if(result.size() <= e.size() && result > e )
    				result = e;
    		}
    		pop_heap (heap.begin(),heap.end()); heap.pop_back();
    	}
    	//
    	return result;
    }
    

Log in to reply
 

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