10244. Shortest Word Distance II

Create a class that, upon initialization, is given a list of words. Implement a method that, given two words firstWord and secondWord, returns the minimum distance (in positions) between these two words in the original list. This method may be called several times with different word pairs.

Examples

Input:
words = ["learn", "code", "repeat", "test", "code"]
WordDistance wd = new WordDistance(words);
wd.closestGap("test", "learn"); 
Output: 3

Input:
wd.closestGap("code", "repeat");
Output: 1

Constraints

  • The list of words contains between 2 and 20,000 elements.
  • Each word consists of 1 to 12 lowercase English letters.
  • Both firstWord and secondWord are guaranteed to be present in the list and are not the same.




Please use Laptop/Desktop or any other large screen to add/edit code.