Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dec983f

Browse files
committed
temp day-16
1 parent 09fb169 commit dec983f

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

‎src/may/day-fourteen/index.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Trie trie = new Trie();
2+
3+
// trie.insert("apple");
4+
// trie.search("apple"); // returns true
5+
// trie.search("app"); // returns false
6+
// trie.startsWith("app"); // returns true
7+
// trie.insert("app");
8+
// trie.search("app"); // returns true
9+
10+
class TrieNode {
11+
private isEnd: boolean = false;
12+
private freq: number = 0;
13+
private child: object = {};
14+
public constructor() {
15+
this.isEnd = false;
16+
this.freq = 0;
17+
this.child = {};
18+
}
19+
}
20+
21+
class Trie extends TrieNode {
22+
public root: TrieNode = undefined;
23+
constructor(trie) {
24+
super();
25+
this.root = new TrieNode();
26+
}
27+
28+
/**
29+
* Inserts a word into the trie.
30+
* @param {string} word
31+
* @return {void}
32+
*/
33+
public insert(word: string): void {
34+
if (word.length) return;
35+
let letter: TrieNode = new TrieNode();
36+
let curNode: TrieNode = new TrieNode();
37+
let i: number = 0;
38+
39+
while (i < word.length) {
40+
letter = word[i];
41+
if (!curNode.child.hadOwnProperty(letter)) {
42+
curNode.child[letter] = new TrieNode();
43+
}
44+
curNode = curNode.child[letter];
45+
}
46+
47+
curNode.isEnd = true;
48+
curNode.freq += 1;
49+
}
50+
51+
/**
52+
* Returns if the word is in the trie.
53+
* @param {string} word
54+
* @return {boolean}
55+
*/
56+
public search(word: string): boolean {}
57+
58+
/**
59+
* Returns if there is any word in the trie that starts with the given prefix.
60+
* @param {string} prefix
61+
* @return {boolean}
62+
*/
63+
public startsWith(prefix) {}
64+
65+
public prefix(word) {
66+
let letter: TrieNode = new TrieNode();
67+
let curNode: TrieNode = new TrieNode();
68+
let i: number = 0;
69+
70+
while (i < word.length) {
71+
letter = word[i];
72+
if (!curNode.child.hasOwnProperty(letter)) {
73+
return null;
74+
curNode = curNode.child[letter];
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /