Skip to content

Commit e220f3f

Browse files
authored
Update shortest-word-distance-iii.cpp
1 parent 9c145ac commit e220f3f

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

C++/shortest-word-distance-iii.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ class Solution {
55
public:
66
int shortestWordDistance(vector<string>& words, string word1, string word2) {
77
int dist = INT_MAX;
8+
bool is_same = (word1 == word2);
89
for (int i = 0, index1 = -1, index2 = -1; i < words.size(); ++i) {
910
if (words[i] == word1) {
10-
if (index1 != -1) {
11+
if (is_same && index1 != -1) {
1112
dist = min(dist, abs(index1 - i));
1213
}
1314
index1 = i;

0 commit comments

Comments
 (0)