-
Notifications
You must be signed in to change notification settings - Fork 0
/
153.py
52 lines (43 loc) · 1.47 KB
/
153.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
"""
Problem:
Find an efficient algorithm to find the smallest distance (measured in number of words)
between any two given words in a string.
For example, given words "hello", and "world" and a text content of "dog cat hello cat
dog dog hello cat world", return 1 because there's only one word "cat" in between the
two words.
"""
def calculate_distance(text: str, word1: str, word2: str) -> int:
word_list = text.split()
length = len(word_list)
distance, position, last_match = None, None, None
# searching for the smallest distance
for i in range(length):
if word_list[i] in (word1, word2):
if last_match in (word_list[i], None):
last_match = word_list[i]
position = i
continue
current_distance = i - position - 1
last_match = word_list[i]
position = i
if distance == None:
distance = current_distance
else:
distance = min(distance, current_distance)
return distance
if __name__ == "__main__":
print(
calculate_distance(
"dog cat hello cat dog dog hello cat world", "hello", "world"
)
)
print(
calculate_distance("dog cat hello cat dog dog hello cat world", "world", "dog")
)
print(calculate_distance("hello world", "hello", "world"))
print(calculate_distance("hello", "hello", "world"))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
"""