Skip to content

Files

Latest commit

 

History

History
39 lines (30 loc) · 2.17 KB

File metadata and controls

39 lines (30 loc) · 2.17 KB

< Previous                  Next >

Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list. Your method will be called repeatedly many times with different parameters. 

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

Related Topics

[Design] [Array] [Hash Table] [Two Pointers] [String]

Similar Questions

  1. Merge Two Sorted Lists (Easy)
  2. Shortest Word Distance (Easy)
  3. Shortest Word Distance III (Medium)