Python DFS Solution


  • 3
    Y
    class Solution(object):
        def findAllConcatenatedWordsInADict(self, words):
            """
            :type words: List[str]
            :rtype: List[str]
            """
            word_set = set(words)
            ans = []
            def helper(w, cur, cnt):
                if cur == len(w):
                    if cnt > 1:
                        return True
                    else:
                        return False
                for i in xrange(cur + 1, len(w) + 1):
                    if w[cur : i] in word_set and helper(w, i, cnt + 1):
                        return True
                return False
            for w in words:
                if helper(w, 0, 0):
                    ans.append(w)
            return ans
    
    

Log in to reply
 

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