-
Notifications
You must be signed in to change notification settings - Fork 43
/
shortest-word-distance.py
70 lines (61 loc) · 2.26 KB
/
shortest-word-distance.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
66
67
68
69
70
"""
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
For example, Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “coding”, word2 = “practice”, return 3. Given word1 = "makes", word2 = "coding", return 1.
Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
"""
# V0
# V1
# http://www.voidcn.com/article/p-epmviyav-qp.html
# IDEA : LOOP
# NOTE : have to consider the deplicated element cases
# e.g.
# Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
# ->
# Given word1 = “coding”, word2 = “practice”, return 3. <---- “coding” index = 3, “practice” index = 0, 3-0 = 3
# Given word1 = "makes", word2 = "coding", return 1. <---- "makes" index = 1,4, "coding" index = 3, so take the min : 4-3 = 1
class Solution(object):
def shortestDistance(self, words, word1, word2):
"""
:type words: List[str]
:type word1: str
:type word2: str
:rtype: int
"""
w1 = [i for i in range(len(words)) if words[i] == word1]
w2 = [i for i in range(len(words)) if words[i] == word2]
return min([abs(i - j) for i in w1 for j in w2])
# V1'
# http://www.voidcn.com/article/p-epmviyav-qp.html
def shortestDistance(self, words, word1, word2):
size = len(words)
index1, index2 = size, size
ans = size
for i in range(size):
if words[i] == word1:
index1 = i
ans = min(ans, abs(index1-index2))
elif words[i] == word2:
index2 = i
ans = min(ans, abs(index1-index2))
return ans
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
# @param {string[]} words
# @param {string} word1
# @param {string} word2
# @return {integer}
def shortestDistance(self, words, word1, word2):
dist = float("inf")
i, index1, index2 = 0, None, None
while i < len(words):
if words[i] == word1:
index1 = i
elif words[i] == word2:
index2 = i
if index1 is not None and index2 is not None:
dist = min(dist, abs(index1 - index2))
i += 1
return dist