forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#4_Count_of_distinct_substrings.cpp
39 lines (36 loc) · 1.06 KB
/
#4_Count_of_distinct_substrings.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
/*
TODO: Count of distinct substrings
? https://practice.geeksforgeeks.org/problems/count-of-distinct-substrings/1
*/
class Node {
private:
Node* links[26];
int cntEndWith = 0, cntPrefix = 0;
public:
bool containKey(char ch) { return (links[ch - 'a'] != nullptr); }
Node* get(char ch) { return links[ch - 'a']; }
void put(char ch, Node* node) { links[ch - 'a'] = node; }
void setEnd() { cntEndWith++; }
void incPre() { cntPrefix++; }
void delEnd() { cntEndWith--; }
void delPre() { cntPrefix--; }
int getEnd() { return cntEndWith; }
int getPre() { return cntPrefix; }
};
int countDistinctSubstring(string s) {
Node* root = new Node();
int cnt = 0, n = s.size();
for(int i=0;i < n;i++) {
string temp = s.substr(i,n - i);
Node* node = root;
for(auto ch : temp) {
if(node->containKey(ch) == false) {
node->put(ch,new Node());
cnt++;
}
node = node->get(ch);
node->incPre();
}
}
return cnt + 1;
}