-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.cpp
77 lines (65 loc) · 2.03 KB
/
Trie.cpp
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
// Trie
// Problem: 1 - https://leetcode.com/problems/longest-common-prefix/
// Problem: 2 - https://leetcode.com/problems/lexicographical-numbers/
// Problem: 3 - https://leetcode.com/problems/implement-trie-prefix-tree/
// Problem: 4 - https://leetcode.com/problems/search-suggestions-system/
// Problem: 5 - https://www.hackerrank.com/challenges/one-month-preparation-kit-sparse-arrays/problem
// ******************************************************
// Linked-list based solution
// ******************************************************
// Change nLetters/strToASCII according to problem!
const int nLetters = 26;
int strToASCII (char ch) { return ch - 'a'; }
char ASCIItoStr(int ch) { return ch + 'a'; }
class TrieNode {
public:
TrieNode *next[nLetters + 1] = { nullptr };
int serial = 0;
int prefixCount = 0;
bool isEndOne = false;
int occurance = 0;
TrieNode(int _x = 0) : serial(_x) {}
};
class Trie {
TrieNode *root;
int nString = 0;
int nNode = 1;
vector<string> lexicoStrings;
public:
Trie() { root = new TrieNode(); }
void insert(string word) {
TrieNode *p = root;
for(char &ch: word) {
int val = strToASCII(ch);
if(p->next[val] == NULL) p->next[val] = new TrieNode(nNode++);
p = p->next[val];
p->prefixCount++;
}
nString++;
p->isEndOne=true;
p->occurance++;
}
void solve () {
// write your code here...
}
private:
TrieNode* _findByKey (string key) {
TrieNode *node = root;
for(char &ch: key) {
int value = strToASCII(ch);
node = node->next[value];
if(node == NULL) break;
}
return node;
}
void _getLexicalOrderList (TrieNode *node, string &cur, vector<string> &res) {
if(node->isEndOne == true) res.push_back(cur);
for(int i = 0; i < nLetters; i++) {
if(node->next[i] != NULL) {
cur += ASCIItoStr(i);
_getLexicalOrderList(node->next[i], cur, res);
cur.pop_back();
}
}
}
};