Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 80a0a6c

Browse files
committed
day 14
1 parent dec983f commit 80a0a6c

File tree

2 files changed

+34
-47
lines changed

2 files changed

+34
-47
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
- Day 11: [Flood Fill](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-eleven/index.ts) :raising_hand:
1515
- Day 12: [Single Element in a Sorted Array](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-twelve/index.ts) :hear_no_evil:
1616
- Day 13: [Remove K Digits](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-thirteen/index.ts) :tongue:
17+
- Day 14: [Implement Trie (Prefix Tree)](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-thirteen/index.ts) :tongue:

src/may/day-fourteen/index.ts

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
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-
101
class TrieNode {
11-
private isEnd: boolean = false;
12-
private freq: number = 0;
13-
private child: object = {};
2+
public root: object = {};
143
public constructor() {
15-
this.isEnd = false;
16-
this.freq = 0;
17-
this.child = {};
4+
this.root;
185
}
196
}
207

218
class Trie extends TrieNode {
22-
public root: TrieNode = undefined;
23-
constructor(trie) {
9+
public root = undefined;
10+
public constructor() {
2411
super();
25-
this.root = new TrieNode();
12+
this.root = new TrieNode().root;
2613
}
2714

2815
/**
@@ -31,48 +18,47 @@ class Trie extends TrieNode {
3118
* @return {void}
3219
*/
3320
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];
21+
let node = this.root;
22+
for (let i = 0; i < word.length; i++) {
23+
node = node[word[i]] = node[word[i]] || {};
24+
if (i === word.length - 1) node.isTerminal = true;
4525
}
46-
47-
curNode.isEnd = true;
48-
curNode.freq += 1;
4926
}
5027

5128
/**
5229
* Returns if the word is in the trie.
5330
* @param {string} word
5431
* @return {boolean}
5532
*/
56-
public search(word: string): boolean {}
33+
public search(word: string, isPrefix?: boolean): boolean {
34+
let node = this.root;
35+
for (let c of word) {
36+
if (!node[c]) return false;
37+
node = node[c];
38+
}
39+
return isPrefix || !!node.isTerminal;
40+
}
5741

5842
/**
5943
* Returns if there is any word in the trie that starts with the given prefix.
6044
* @param {string} prefix
6145
* @return {boolean}
6246
*/
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-
}
47+
public startsWith(prefix: string): boolean {
48+
return this.search(prefix, true);
7749
}
7850
}
51+
52+
const trie = new Trie();
53+
54+
trie.insert('apple');
55+
const a = trie.search('apple'); // returns true
56+
const b = trie.search('app'); // returns false
57+
const c = trie.startsWith('app'); // returns true
58+
trie.insert('app');
59+
const d = trie.search('app'); // returns true
60+
61+
console.log('a', a);
62+
console.log('b', b);
63+
console.log('c', c);
64+
console.log('d', d);

0 commit comments

Comments
 (0)