File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
docs/Leetcode_Solutions/Python Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -104,3 +104,36 @@ class Solution:
104104****** - 时间复杂度: O(words_num * word_len)****** - 空间复杂度: O(words_num * word_len)******
105105
106106
107+ 将这道题转化为第139题的解法,一个word肯定只能由比它更短的word组成,因此我们先按照word长度将words排个序
108+
109+ 然后遍历,看当前的word是否能由它之前的words组成,这个地方就是第139题的解法了,但是稍微有一点点区别,那就是我们这里如果当前word必须由两个及两个以上word组成才可以,也就是说如果当前word的长度小于2的话,肯定不能由两个及两个以上word组成
110+
111+ beats 8.57%
112+
113+ ``` python
114+ class Solution :
115+ def findAllConcatenatedWordsInADict (self , words ):
116+ """
117+ :type words: List[str]
118+ :rtype: List[str]
119+ """
120+ if not words or len (words) == 0 :
121+ return []
122+ res = []
123+ words.sort(key = lambda s : len (s))
124+ pre_words_lookup = set ()
125+ for i in range (len (words)):
126+ if self .canConcatenated(words[i], pre_words_lookup):
127+ res.append(words[i])
128+ pre_words_lookup.add(words[i])
129+ return res
130+
131+ def canConcatenated (self , s , wordDict ):
132+ if len (s) <= 1 :
133+ return False
134+ ok = [True ]
135+ for i in range (1 , len (s)+ 1 ):
136+ ok += [any (ok[j] and s[j:i] in wordDict for j in range (i))]
137+ return ok[- 1 ]
138+ ```
139+
You can’t perform that action at this time.
0 commit comments