forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbefore-and-after-puzzle.py
30 lines (26 loc) · 1002 Bytes
/
before-and-after-puzzle.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
# Time: O(l * rlogr) , l is the max length of phrases
# , r is the number of result, could be up to O(n^2)
# Space: O(l * (n + r)), n is the number of phrases
import collections
class Solution(object):
def beforeAndAfterPuzzles(self, phrases):
"""
:type phrases: List[str]
:rtype: List[str]
"""
lookup = collections.defaultdict(list)
for i, phrase in enumerate(phrases):
right = phrase.rfind(' ')
word = phrase if right == -1 else phrase[right+1:]
lookup[word].append(i)
result_set = set()
for i, phrase in enumerate(phrases):
left = phrase.find(' ')
word = phrase if left == -1 else phrase[:left]
if word not in lookup:
continue
for j in lookup[word]:
if j == i:
continue
result_set.add(phrases[j] + phrase[len(word):])
return sorted(result_set)