-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path648-replace-words.js
More file actions
91 lines (77 loc) · 2.71 KB
/
648-replace-words.js
File metadata and controls
91 lines (77 loc) · 2.71 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word successor. For example, when the root "an" is followed by the successor word "other", we can form a new word "another".
Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the successors in the sentence with the root forming it. If a successor can be replaced by more than one root, replace it with the root that has the shortest length.
Return the sentence after the replacement.
Example 1:
Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"
Example 2:
Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
Output: "a a b c"
*/
/**
* @param {string[]} dictionary
* @param {string} sentence
* @return {string}
*/
// Approach 1: Hashset
var replaceWords = function(dictionary, sentence) {
const rootset = new Set();
for(d of dictionary) {
rootset.add(d);
}
let ans = "";
const words = sentence.split(" ");
for(let word of words) {
let prefix = "";
for(let i = 1; i <= word.length; ++i) {
prefix = word.substring(0,i);
if(rootset.has(prefix)) {
break;
}
}
if (ans.length > 0) // If it's not the first word in sentence, add a space
ans += " "
ans += prefix;
}
return ans;
};
/* Approach 2: Trie
Time Complexity: O(N) where N is the length of the sentence. Every query of a word is in linear time.
Space Complexity: O(N), the size of our trie.
*/
const Node = function(x) {
this.val = x;
this.children = new Map();
}
var replaceWords = function(dictionary, sentence) {
const res = [], head = new Node('');
for(word of dictionary) {
let curr = head;
for(c of word) {
if (!curr.children.has(c)) {
curr.children.set(c, new Node(c));
}
curr = curr.children.get(c);
}
curr.children.set('.', new Node('.'));
}
const words = sentence.split(" ");
for(let word of words) {
let curr = head, newWord = [];
for(let c of word) {
if (!curr.children.has(c)) break;
if (curr.children.has('.')) break;
newWord.push(c);
curr = curr.children.get(c);
}
if (newWord.length) {
res.push(curr.children.has('.') ? newWord.join('') : word);
} else {
res.push(word);
}
}
return res.join(' ');
};
const result = replaceWords(["cat","bat","rat"], "the cattle was rattled by the battery");
console.log(result);