-
Notifications
You must be signed in to change notification settings - Fork 0
/
022.py
53 lines (41 loc) · 1.51 KB
/
022.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
"""
Problem:
Given a dictionary of words and a string made up of those words (no spaces), return the
original sentence in a list. If there is more than one possible reconstruction, return
any of them. If there is no possible reconstruction, then return null.
For example, given the set of words 'quick', 'brown', 'the', 'fox', and the string
"thequickbrownfox", you should return ['the', 'quick', 'brown', 'fox'].
Given the set of words 'bed', 'bath', 'bedbath', 'and', 'beyond', and the string
"bedbathandbeyond", return either ['bed', 'bath', 'and', 'beyond] or
['bedbath', 'and', 'beyond'].
"""
from typing import List
def get_sentence_split(word_list: List[str], string: str) -> List[str]:
word_set = set()
buffer = ""
words_found = []
# populating the set with the words for O(1) access
for word in word_list:
word_set.add(word)
# searching for words in the string
for char in string:
buffer += char
if buffer in word_set:
words_found.append(buffer)
buffer = ""
if len(words_found) == 0:
return None
return words_found
if __name__ == "__main__":
print(get_sentence_split(["quick", "brown", "the", "fox"], "thequickbrownfox"))
print(
get_sentence_split(
["bed", "bath", "bedbath", "and", "beyond"], "bedbathandbeyond"
)
)
print(get_sentence_split(["quick", "brown", "the", "fox"], "bedbathandbeyond"))
"""
SPECS:
TIME COMPLEXITY: O(characters_in_input_string)
SPACE COMPLEXITY: O(words)
"""