-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ15_longest_word.py
38 lines (30 loc) · 1 KB
/
Q15_longest_word.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import unittest
def longest_word(words):
words.sort(reverse=True, key=lambda x: len(x))
for word in words:
dp = [None] * len(word)
if can_be_split(word, 0, True, words, dp):
return word
def can_be_split(word, idx, is_original_word, words, dp):
if idx == len(word):
return not is_original_word
is_valid_split = False
if dp[idx] is None:
for end in range(idx, len(word) + 1):
if word[idx:end] in words and can_be_split(word, end, False, words, dp):
is_valid_split = True
break
dp[idx] = is_valid_split
return dp[idx]
class Test(unittest.TestCase):
def test_loongest_word(self):
self.assertEqual(
"dogwalker",
longest_word(
["cat", "banana", "dog", "nana", "walk", "walker", "dogwalker"]
),
)
self.assertEqual(
"dogwalk",
longest_word(["cat", "banana", "dog", "nana", "walk", "dogwalk"]),
)