-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path243.shortest-word-distance.js
50 lines (39 loc) · 1.05 KB
/
243.shortest-word-distance.js
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
/*
* @lc app=leetcode id=243 lang=javascript
*
* [243] Shortest Word Distance
*/
/**
* @param {string[]} words
* @param {string} word1
* @param {string} word2
* @return {number}
*/
/*
["practice", "makes", "perfect", "coding", "makes"].
know more question
1. words can be duplicates, "makes " exist twice
2. no moving to the top at the end
["practice", "makes", "perfect", "coding", "makes"]
Input: word1 = “coding”(3), word2 = “practice”(0)
Output: 3
Input: word1 = "makes"(4), word2 = "coding"(3)
Output: 1
Distance definition
abs(index1-index2)
*/
var shortestDistance = function(words, word1, word2) {
//one pass, one index, time: O(N), space:O(1)
var potentialWordIndex = -1,
ans = Number.MAX_SAFE_INTEGER;
for (var i = 0; i < words.length; i++) {
if (words[i] === word1 || words[i] === word2) {
if (potentialWordIndex !== -1 && words[potentialWordIndex] !== words[i]) {
//finding the another word0
ans = Math.min(i - potentialWordIndex, ans);
}
potentialWordIndex = i;
}
}
return ans;
};