-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordDistance.js
57 lines (45 loc) · 1.36 KB
/
WordDistance.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
51
52
53
54
55
56
57
'use strict'
/*
You have a large text file containing words.
Given any two words, find the shortest distance(in terms of number of words) between them in the file.
If the operation will be rpeated many rimes for the same file (but different pair of words), can you optimize your solution?
*/
class LocationPair{
constructor(first, second){
this.setLocations(first, second)
}
setLocations(first, second){
this.location1 = first
this.location2 = second
}
distance(){
return Math.abs(this.location1 - this.location2)
}
isValid(){
return this.location1 >= 0 && this.location2 >= 0
}
updateWithMin(loc){
if (!this.isValid() || loc.distance() < this.distance()){
this.setLocations(loc.location1, loc.location2)
}
}
}
class WordDistance{
findClosest(words, word1, word2){
let best = new LocationPair(-1, -1)
let current = new LocationPair(-1, -1)
for (let i = 0; i < words.length; i++){
let word = words[i]
if (word === word1){
current.location1 = i
best.updateWithMin(current)
}
else if (word === word2){
current.location2 = i
best.updateWithMin(current)
}
}
return best
}
}
module.exports = WordDistance