Skip to content

Commit 9c145ac

Browse files
authored
Update shortest-word-distance-iii.py
1 parent 8c13dab commit 9c145ac

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Python/shortest-word-distance-iii.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ class Solution:
88
# @return {integer}
99
def shortestWordDistance(self, words, word1, word2):
1010
dist = float("inf")
11+
is_same = (word1 == word2)
1112
i, index1, index2 = 0, None, None
1213
while i < len(words):
1314
if words[i] == word1:
14-
if index1 is not None:
15+
if is_same and index1 is not None:
1516
dist = min(dist, abs(index1 - i))
1617
index1 = i
1718
elif words[i] == word2:

0 commit comments

Comments
 (0)