-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
48 lines (39 loc) · 1 KB
/
index.ts
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
export class Node {
isLeaf: boolean = false;
children: Node[];
constructor(n: number) {
this.children = new Array(n).fill(null);
}
}
function add(root: Node, string: string) {
let tmp = root;
for (const c of string.toLowerCase()) {
const index = c.charCodeAt(0) - 97;
if (!tmp.children[index]) {
tmp.children[index] = new Node(26);
}
tmp = tmp.children[index];
}
tmp.isLeaf = true;
}
function find(root: Node, string: string) {
let tmp = root;
for (const c of string.toLowerCase()) {
const index = c.charCodeAt(0) - 97;
tmp = tmp.children[index];
if (!tmp) {
return false;
}
}
return tmp.isLeaf;
}
const decoder = new TextDecoder("utf-8");
// Dictionary used taken from
// wget https://cdn.cs50.net/2019/fall/psets/5/speller/speller.zip
const data = await Deno.readFile("./dictionary");
const root = new Node(26);
// Load a dictionary
for (const line of decoder.decode(data).split("\n")) {
add(root, line);
}
console.log(find(root, "hello"));