-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path924.shortest-word-distance.py
More file actions
52 lines (47 loc) · 1.31 KB
/
924.shortest-word-distance.py
File metadata and controls
52 lines (47 loc) · 1.31 KB
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
# Tag: Enumerate, Array
# Time: O(N)
# Space: O(1)
# Ref: Leetcode-243
# Note: -
# Given a list of `words` and two words `word1` and `word2`, return the shortest distance between these two words in the list.
#
# **Example 1:**
# ```
# Input:["practice", "makes", "perfect", "coding", "makes"],"coding","practice"
# Output:3
# Explanation:index("coding") - index("practice") = 3
# ```
#
#
# **Example 2:**
# ```
# Input:["practice", "makes", "perfect", "coding", "makes"],"makes","coding"
# Output:1
# Explanation:index("makes") - index("coding") = 1
# ```
#
# You may assume that `word1 does not equal to word2`, and word1 and word2 are `both` in the list.
from typing import (
List,
)
class Solution:
"""
@param words: a list of words
@param word1: a string
@param word2: a string
@return: the shortest distance between word1 and word2 in the list
"""
def shortest_distance(self, words: List[str], word1: str, word2: str) -> int:
# Write your code here
n = len(words)
res = n
a = -1
b = -1
for i in range(n):
if words[i] == word1:
a = i
elif words[i] == word2:
b = i
if a >= 0 and b >= 0:
res = min(res, abs(a - b))
return res