-
-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathShortestWordDistanceIII.java
More file actions
73 lines (67 loc) · 2.84 KB
/
ShortestWordDistanceIII.java
File metadata and controls
73 lines (67 loc) · 2.84 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
72
73
package com.leetcode.arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Level: Easy
* Problem Link: https://leetcode.com/problems/shortest-word-distance-iii/
* Problem Description:
* This is a follow-up problem of {@link ShortestWordDistance}. The only difference is that now word1 could be the
* same as word2.
* <p>
* Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
* word1 and word2 may be the same and they represent two individual words in the list.
* <p>
* For example,
* Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
* Given word1 = “makes”, word2 = “coding”, return 1.
* Given word1 = "makes", word2 = "makes", return 3.
* <p>
* Note: You may assume word1 and word2 are both in the list. If they are same then it's guaranteed that there are
* two occurrences of the same.
*
* @author rampatra
* @since 2019-07-31
*/
public class ShortestWordDistanceIII {
/**
* Time Complexity:
* Space Complexity:
* TODO
*
* @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;
boolean isSameWord = word1.equals(word2);
for (int i = 0; i < words.length; i++) {
// Update indexWord1 only if: words are different OR this is the first occurrence
if (words[i].equals(word1) && (!isSameWord || indexWord1 == -1)) {
indexWord1 = i;
}
// Update indexWord2 only if: words are different OR this is not the first occurrence
if (words[i].equals(word2) && (!isSameWord || i != indexWord1)) {
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"},
"makes", "makes"));
assertEquals(3, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
"coding", "practice"));
assertEquals(3, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
"practice", "coding"));
assertEquals(1, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
"makes", "coding"));
assertEquals(1, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
"makes", "perfect"));
}
}