Example #1
0
std::vector<std::string> findAllConcatenatedWordsInADict(std::vector<std::string>& words) 
{
	std::sort(words.begin(), words.end(), compare);
	std::unordered_set<std::string> set;

	std::vector<std::string> result;
	for (const std::string& s : words)//注意这里,考虑当前元素的时候,不能把当前元素加入到hashset中
	{
		if (canForm(s, set))
			result.push_back(s);
		set.insert(s);
	}

	return result;
}
Example #2
0
    vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
        sort(words.begin(), words.end(), [](const auto &lhs, const auto &rhs) {
                return lhs.size() < rhs.size();
            });
        vector<string> ans;
        unordered_set<string> preWords;
        int n = words.size();

        for(auto &word: words) {
            if(canForm(word, preWords)) {
                ans.push_back(word);
            }
            preWords.insert(word);
        }
        return ans;
    }