472. Concatenated Words

class Solution {
public:
    vector<string> res;
    vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
        unordered_set<string> s(words.begin(), words.end());
        for (auto & w : words)
            if (helper(s, w)) 
                res.push_back(w);
        return res;
    }
    bool helper(unordered_set<string>& s, const string& w) {
        for (int i = 1; i < w.length(); i++) {
            if (s.find(w.substr(0, i)) == s.end())  continue;
            
            string right = w.substr(i);
            if (s.find(right) != s.end() || helper(s, right))
                return true;
        }
        return false;
    }
};
原文地址:https://www.cnblogs.com/JTechRoad/p/10087784.html