美文网首页
8.23 - hard - 91

8.23 - hard - 91

作者: 健时总向乱中忙 | 来源:发表于2017-08-23 23:02 被阅读0次

    472. Concatenated Words

    利用Trie做出一个TLE的版本。其实简单的利用recursion就可以了。

    class Solution(object):
        def findAllConcatenatedWordsInADict(self, words):
            """
            :type words: List[str]
            :rtype: List[str]
            """
            res = []
            s = set(words)
            for word in words:
                s.remove(word);
                if self.check(word,s):
                    res.append(word)
                s.add(word)
            return res
        
        def check(self, word, s):
            if word in s:
                return True
            l = len(word) - 1
            while l >= 1:
                if word[0:l] in s and self.check(word[l:],s):
                    return True
                l -= 1
            return False
            
    

    相关文章

      网友评论

          本文标题:8.23 - hard - 91

          本文链接:https://www.haomeiwen.com/subject/ecojdxtx.html