1
1
package com .leetcode .maps ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .HashMap ;
5
+ import java .util .List ;
6
+ import java .util .Map ;
7
+
8
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
9
+
3
10
/**
4
11
* Level: Medium
5
12
* Problem Link: https://leetcode.com/problems/shortest-word-distance-ii/ (premium)
6
13
* Problem Description:
7
- * This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and
8
- * your method will be called repeatedly many times with different parameters. How would you optimize it?
9
- * <p>
10
14
* Design a class which receives a list of words in the constructor, and implements a method that takes two words
11
- * word1 and word2 and return the shortest distance between these two words in the list.
15
+ * word1 and word2 and return the shortest distance between these two words in the list. Your method will be called
16
+ * repeatedly many times with different parameters. For a simpler variant, see {@link com.leetcode.arrays.ShortestWordDistance}.
12
17
* <p>
13
- * Example 1 :
18
+ * Examples :
14
19
* Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
15
- * Given word1 = "coding”, word2 = "practice”, return 3.
16
- * Given word1 = "makes", word2 = "coding", return 1.
17
20
* <p>
18
- * Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
21
+ * Input1: word1 = “coding”, word2 = “practice”
22
+ * Output1: 3
23
+ * <p>
24
+ * Input2: word1 = "makes", word2 = "coding"
25
+ * Output2: 1
19
26
* <p>
20
- * Lastly, for a simpler variant, see {@link com.leetcode.arrays.ShortestWordDistance} .
27
+ * Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list .
21
28
*
22
29
* @author rampatra
23
30
* @since 2019-07-31
24
31
*/
25
32
public class ShortestWordDistanceII {
26
33
27
- public static int findShortestDistance (String [] words , String word1 , String word2 ) {
28
- return -1 ;
34
+ private String [] words ;
35
+ private Map <String , List <Integer >> wordsToIndexesMap ;
36
+
37
+ ShortestWordDistanceII (String [] words ) {
38
+ this .words = words ;
39
+ this .wordsToIndexesMap = getWordsToIndexesMap ();
29
40
}
30
41
31
- public static void main (String [] args ) {
42
+ public int findShortestDistance (String word1 , String word2 ) {
43
+ return findShortestDistance (wordsToIndexesMap .get (word1 ), wordsToIndexesMap .get (word2 ));
44
+ }
32
45
46
+ private int findShortestDistance (List <Integer > indexes1 , List <Integer > indexes2 ) {
47
+ int minDistance = Integer .MAX_VALUE ;
48
+
49
+ for (int i = 0 , j = 0 ; i < indexes1 .size () && j < indexes2 .size (); ) {
50
+ if (indexes1 .get (i ) <= indexes2 .get (j )) {
51
+ minDistance = Math .min (minDistance , Math .abs (indexes1 .get (i ) - indexes2 .get (j )));
52
+ i ++;
53
+ } else if (indexes1 .get (i ) > indexes2 .get (j )) {
54
+ minDistance = Math .min (minDistance , Math .abs (indexes1 .get (i ) - indexes2 .get (j )));
55
+ j ++;
56
+ }
57
+ }
58
+
59
+ return minDistance ;
60
+ }
61
+
62
+ private Map <String , List <Integer >> getWordsToIndexesMap () {
63
+ Map <String , List <Integer >> wordsToIndexesMap = new HashMap <>();
64
+
65
+ for (int i = 0 ; i < words .length ; i ++) {
66
+ wordsToIndexesMap .putIfAbsent (words [i ], new ArrayList <>());
67
+ wordsToIndexesMap .get (words [i ]).add (i );
68
+ }
69
+ return wordsToIndexesMap ;
70
+ }
71
+
72
+ public static void main (String [] args ) {
73
+ ShortestWordDistanceII shortestWordDist = new ShortestWordDistanceII (new String []{"practice" , "makes" , "perfect" , "coding" , "makes" });
74
+ assertEquals (1 , shortestWordDist .findShortestDistance ("coding" , "makes" ));
75
+ assertEquals (1 , shortestWordDist .findShortestDistance ("perfect" , "makes" ));
76
+ assertEquals (1 , shortestWordDist .findShortestDistance ("practice" , "makes" ));
77
+ assertEquals (1 , shortestWordDist .findShortestDistance ("makes" , "practice" ));
78
+ assertEquals (3 , shortestWordDist .findShortestDistance ("coding" , "practice" ));
79
+ assertEquals (0 , shortestWordDist .findShortestDistance ("coding" , "coding" ));
80
+ assertEquals (0 , shortestWordDist .findShortestDistance ("makes" , "makes" ));
33
81
}
34
- }
82
+ }
0 commit comments