-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGFG_Count_of_Distinct_Substrings.cpp
More file actions
51 lines (43 loc) · 1001 Bytes
/
Copy pathGFG_Count_of_Distinct_Substrings.cpp
File metadata and controls
51 lines (43 loc) · 1001 Bytes
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
struct Node{
Node* links[26];
bool containsKey(char ch){
return (links[ch - 'a'] != NULL);
}
void put(char ch, Node* node){
links[ch - 'a'] = node;
}
Node* get(char ch){
return links[ch - 'a'];
}
};
class Trie{
private:
Node* root;
public:
int count;
Trie(){
root = new Node();
count = 0;
}
void insert(string s){
Node* node = root;
for(int i=0; i<s.size(); i++){
if(!node->containsKey(s[i])){
count++;
node->put(s[i], new Node());
}
node = node->get(s[i]);
}
}
};
class Solution {
public:
int countSubs(string& s) {
Trie* trie = new Trie();
int n = s.size();
for(int i=0; i<n; i++){
trie->insert(s.substr(i, n-i));
}
return trie->count;
}
};