LeetCode 472. Concatenated Words - Bottom Up DP - Python 3 - for Coding Interviews

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

class Solution:
def findAllConcatenatedWordsInADict(self, words: List[str]) -> List[str]:
words_set = set(words)
answer = []
for word in words:
length = len(word)
dp = [True]+[False]*length
for i in range(1, length+1):
for j in range(0, i):
if i==length and j==0:
continue
if not dp[i]:
dp[i] = dp[j] & (word[j:i] in words_set)
if dp[length]:
answer.append(word)
return answer

alicecodeland
Автор

Very nice DP solution to this interesting Leetcode problem.
I've watched 3 different approaches to solve LC 472 - Prefix Tree, DFS and DP.
Yours is much easier to understand and write code than the others.

CostaKazistov
visit shbcf.ru