Skip to content

Commit 90ebf07

Browse files
author
Joseph Luce
authored
Update Longest_movie_title.md
1 parent f455315 commit 90ebf07

File tree

1 file changed

+88
-33
lines changed

1 file changed

+88
-33
lines changed
Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,68 @@
1-
# SOLUTION
1+
# QUESTION
2+
Return the longest movie title found by successively matching the last and first words in each title, joining to form new titles, given an array containing a list of movie titles.
3+
4+
```
5+
For example:
6+
Input: ['OF MICE AND MEN', 'BLACK MASS', 'MEN IN BLACK"]
7+
Output: 'OF MICE AND MEN IN BLACK MASS'
8+
9+
Explanation:
10+
'OF MICE AND MEN' and 'MEN IN BLACK' join to form 'OF MICE AND MEN IN BLACK'.
11+
You could further join 'OF MICE AND MEN IN BLACK' wth 'BLACK MASS' to form 'OF MICE AND MEN IN BLACK MASS'.
212
```
3-
import copy
13+
14+
# EXPLAINATION
15+
This is another graph problem. Make an adjacent list for each title. You may need need a hash table to help you perform this, mainly a hash table with first word as the key and the title as the value.
16+
17+
Then create a method that goes through all the titles and performs a DFS. When using a DFS on a graph, you will need a stack/recursion to keep the current longest title found so far. For each neighbor, we will call a DFS and concat the result from that search to our title and check if that new result is better than our result found so far.
18+
19+
Also, we will need a different visited set for each DFS, this is to avoid cyclic searches and to avoid doing useless searches.
20+
Say you have 'a b', 'b c', 'c d', 'd e'. If you begun your search at 'c d', you will get a result of 'c d d e'. 'c d' and 'd e' will be marked as visited but when you visit 'd e ' again, you should not begin a new DFS with this title. But for 'b c' or 'a b' its fine to start a new DFS search.
21+
22+
Worst case occurs when the titles you start your DFS search will always create the next smallest title. Therefore it would be O(n^2), where N is the number of titles.
23+
24+
You can avoid O(n^2) by using memoization. Therefore, you can get O(n). Also if there is a recursive solution, there will be a dp solution. You can also use your memoization as your global visited set in this case.
25+
26+
# SOLUTION
27+
```import copy
428
529
def get_longest_title(titles):
630
adj_list = create_adj_list(titles)
7-
global_visit = set()
831
longest_title = list()
932
memo = dict()
1033
for title in adj_list.keys():
11-
if title not in global_visit:
34+
if title not in memo:
1235
local_visit = set()
13-
local_title = list()
14-
local_result = visit_title(title,
15-
local_title,
36+
local_result = visit_title(title,
1637
adj_list,
17-
local_visit,
18-
global_visit,
38+
local_visit,
1939
memo)
2040
if len(local_result) > len(longest_title):
2141
longest_title = copy.deepcopy(local_result)
22-
return concat_title(longest_title)
42+
return concat_title(adj_list, longest_title)
2343
24-
def concat_title(titles):
44+
def concat_title(adj_list, titles):
2545
if len(titles) == 0:
2646
return ''
27-
result = ''
28-
result = titles[0]
47+
result = list()
48+
result.append(titles[0])
2949
for index in range(1, len(titles)):
3050
title = titles[index]
31-
sub_title = title[1:]
51+
words_in_title = title.split()
52+
sub_title = words_in_title[1:]
3253
result += sub_title
33-
return result
54+
if is_cyclic(adj_list, titles):
55+
result = result[:-1]
56+
return ' '.join(result)
3457
58+
def is_cyclic(adj_list, titles):
59+
if len(titles) <= 1:
60+
return False
61+
last_title = titles[-1]
62+
first_title = titles[0]
63+
if first_title in adj_list[last_title]:
64+
return True
65+
return False
3566
3667
def create_adj_list(titles):
3768
firstWord_to_titles_hash = dict()
@@ -54,35 +85,59 @@ def create_adj_list(titles):
5485
result[title] = list()
5586
return result
5687
57-
def visit_title(title, stack, adj_list, local_visit, global_visit, memo):
88+
def visit_title(title, adj_list, local_visit, memo):
5889
if title in memo:
5990
return memo[title]
6091
result = list()
6192
if title not in local_visit:
93+
result.append(title)
6294
local_visit.add(title)
63-
stack.append(title)
6495
neighbors = adj_list[title]
6596
for next_title in neighbors:
66-
local_result = visit_title(next_title,
67-
stack,
97+
local_result = visit_title(next_title,
6898
adj_list,
69-
local_visit,
70-
global_visit,
99+
local_visit,
71100
memo)
72-
if len(local_result) > len(result):
73-
result = copy.deepcopy(local_result)
74-
if len(stack) > len(result):
75-
result = copy.deepcopy(stack)
76-
stack.pop()
101+
new_result = [title] + local_result
102+
if len(new_result) > len(result):
103+
result = new_result
77104
local_visit.remove(title)
78-
memo[title] = result
105+
memo[title] = copy.deepcopy(result)
79106
return result
80-
107+
81108
Input = ['OF MICE AND MEN', 'BLACK MASS', 'MEN IN BLACK']
82-
#Input = ['a b', 'b c', 'c d', 'c e', 'e f']
83-
#Input = ['a b', 'b c', 'c a']
109+
assert(get_longest_title(Input) == 'OF MICE AND MEN IN BLACK MASS')
110+
111+
Input = []
112+
assert(get_longest_title(Input) == '')
113+
114+
Input = ['a']
115+
assert(get_longest_title(Input) == 'a')
116+
117+
Input = ['a', 'b']
118+
assert(get_longest_title(Input) == 'a' or get_longest_title(Input) == 'b')
119+
120+
Input = ['a b', 'b c', 'c d', 'c e', 'e f']
121+
assert(get_longest_title(Input) == 'a b c e f')
122+
84123
Input = ['e f', 'c e', 'c d','b c', 'a b']
85-
#Input = ['c d','b c', 'a b', 'e f', 'c e']
86-
87-
print get_longest_title(Input)
124+
assert(get_longest_title(Input) == 'a b c e f')
125+
126+
Input = ['c d','b c', 'a b', 'e f', 'c e']
127+
assert(get_longest_title(Input) == 'a b c e f')
128+
129+
Input = ['a b', 'b c', 'c a']
130+
assert(get_longest_title(Input) == 'a b c' \
131+
or get_longest_title(Input) == 'b c a' \
132+
or get_longest_title(Input) == 'c a b')
133+
134+
Input = ['c a', 'b c', 'a b']
135+
assert(get_longest_title(Input) == 'a b c' \
136+
or get_longest_title(Input) == 'b c a' \
137+
or get_longest_title(Input) == 'c a b')
138+
139+
Input = ['b c', 'a b', 'c a']
140+
assert(get_longest_title(Input) == 'a b c' \
141+
or get_longest_title(Input) == 'b c a' \
142+
or get_longest_title(Input) == 'c a b')
88143
```

0 commit comments

Comments
 (0)