shortestDistance
to INT_MAX
and prevIndex
to -1
.wordsDict
with index i
.
a. If the current word is word1
or word2
, proceed to the next step. Otherwise, continue to the next iteration.
b. If prevIndex
is not -1
and either word1
is equal to word2
or the word at prevIndex
is not the same as the current word, update shortestDistance
with the minimum of its current value and the difference between i
and prevIndex
.
c. Set prevIndex
to the current index i
.shortestDistance
after the loop ends.