-
Notifications
You must be signed in to change notification settings - Fork 39
/
longest_word.py
65 lines (58 loc) · 1.67 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
# Date: 2020-12-10
#
# Description:
# Given a list of words, write a program to find the longest word made of other
# words in the list.
#
# EXAMPLE:
# Input: [cat, banana, dog, nana, walk, walker, dogwalker]
# Output: dogwalker
#
# Approach:
# Idea is taken from, how to solve this if we were asked to find pair of
# words which makes a other word. Below approach is followed
# - Sort words with respect to length in descending order
# - Fill all words in map, for easy lookup
# - Scan words, starting from longest(W) and try to find if this can be formed
# using other words. To find this split W at all possible locations calling
# left and right substrings
# - If left is present in map, recursively check if right is also present in
# the map
#
# Complexity:
# O(nlogn + L^2) time and O(n) space
# n = number of strings
# L = average length of string
def length_sorted(word):
return len(word)
def can_build_word(word, is_original, _map):
if not is_original and word in _map:
return _map[word]
for i in range(1, len(word)):
left = word[:i]
right = word[i:]
if left in _map and _map[left] and can_build_word(right, False, _map):
return True
_map[word] = False
return False
def longest_word(words):
sorted_words = sorted(words, reverse=True, key=length_sorted)
_map = {}
for word in sorted_words:
_map[word] = True
for word in sorted_words:
if can_build_word(word, True, _map):
return word
return ''
words = [
'cat',
'banana',
'dog',
'nana',
'walk',
'walker',
'bananadogcatty',
'dogwalker'
]
assert longest_word(words) == 'dogwalker'