bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 245. Shortest Word Distance III

245. Shortest Word Distance III

Leetcode Solutions

Two Pointer Approach

  1. Initialize shortestDistance to INT_MAX and prevIndex to -1.
  2. Iterate over the array 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.
  3. Return shortestDistance after the loop ends.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...