/
32_string_deletion.js
executable file
·79 lines (59 loc) · 1.44 KB
/
32_string_deletion.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Given a string and a dictionary HashSet, write a function to determine the minimum number of characters to delete to make a word.
eg.
dictionary: [“a”, “aa”,”aaa”]
query: “abc”
output: 2
abc
/ | \
ab ac bc
/ \ / \ / \
a b a c b c
query: "abc"
dictionary: ["a", "aa", "aaa"]
queue: [bc, ac, ab]
queueElements: [bc, ac, ab]
s: abc
i:0 -> 2
sub: 0,0 + 1,3 -> 0,2 + 3,3
Time :
O (n * n-1 * n-2 * ... * 1) = o(n!)
*/
class Queue {
constructor() {
this.data = [];
}
enqueue(input) {
this.data.push(input);
}
dequeue() {
return this.data.shift();
}
isEmpty() {
return (!this.data.length > 0);
}
}
function deleteToMakeAWord(dictionary, query) {
let queue = new Queue();
let queueElements = new Set();
queue.enqueue(query);
queueElements.add(query);
while (!queue.isEmpty()) {
s = queue.dequeue();
queueElements.delete(s);
if (dictionary.has(s)) return query.length - s.length;
for (let i = 0; i < s.length; i++) {
let sub = s.substring(0, i) + s.substring(i + 1, s.length);
if (!queueElements.has(sub) && sub.length > 0) {
queue.enqueue(sub);
queueElements.add(sub);
}
}
}
return -1;
}
let HashSet = new Set();
HashSet.add("a");
HashSet.add("aa");
HashSet.add("aaa");
deleteToMakeAWord(HashSet, "abc");