forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestWordDistance.java
More file actions
71 lines (64 loc) · 2.6 KB
/
ShortestWordDistance.java
File metadata and controls
71 lines (64 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.leetcode.arrays;
import com.leetcode.hashtables.ShortestWordDistanceII;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Level: Easy
* Problem Link: https://leetcode.com/problems/shortest-word-distance/
* Problem Description:
* Given a list of words and two words word1 and word2, return the shortest distance between these two words in the
* list of words.
*
* Example 1:
* Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
* Given word1 = "coding", word2 = "practice", return 3.
* Given word1 = "makes", word2 = "coding", return 1.
*
* Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
*
* Lastly, for a more complex variant, see {@link ShortestWordDistanceII}.
*
* @author rampatra
* @since 2019-07-31
*/
public class ShortestWordDistance {
/**
* Time Complexity:
* Space Complexity:
* Runtime: <a href="https://leetcode.com/submissions/detail/248572791/">1 ms</a>.
*
* @param words
* @param word1
* @param word2
* @return
*/
public static int findShortestDistance(String[] words, String word1, String word2) {
int indexWord1 = -1;
int indexWord2 = -1;
int minDistance = Integer.MAX_VALUE;
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) {
indexWord1 = i;
} else if (words[i].equals(word2)) {
indexWord2 = i;
}
if (indexWord1 != -1 && indexWord2 != -1) {
minDistance = Math.min(minDistance, Math.abs(indexWord1 - indexWord2));
}
}
return minDistance;
}
public static void main(String[] args) {
assertEquals(3, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
"practice", "coding"));
assertEquals(3, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
"coding", "practice"));
assertEquals(1, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
"makes", "coding"));
assertEquals(1, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
"makes", "perfect"));
assertEquals(0, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
"perfect", "perfect"));
assertEquals(0, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
"makes", "makes"));
}
}