forked from vns9/binarysearch.com
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLongest Prefix Sequence.cpp
More file actions
42 lines (38 loc) · 1.06 KB
/
Longest Prefix Sequence.cpp
File metadata and controls
42 lines (38 loc) · 1.06 KB
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
// https://binarysearch.com/problems/Longest-Prefix-Sequence
class TrieNode{
public:
TrieNode *child[26] = {nullptr};
bool isend=false;
TrieNode(){};
};
void insert(string word, TrieNode* curr) {
for(int i=0;i<word.size();i++){
if(curr->child[word[i]-'a']==nullptr) curr->child[word[i]-'a'] = new TrieNode();
curr=curr->child[word[i]-'a'];
if(i+1==word.size()) curr->isend=true;
}
}
int ans=1;
void traverse(TrieNode* curr, TrieNode* parent, int prevSequenceLength){
if(curr==nullptr) return;
int currLength = 0;
if(curr->isend){
currLength=1;
}
if(parent && parent->isend && curr->isend){
currLength = prevSequenceLength+1;
ans=max(ans,currLength);
}
for(auto nextPtr: curr->child){
traverse(nextPtr, curr, currLength);
}
}
int solve(vector<string>& words) {
TrieNode* root = new TrieNode();
for(auto word: words) insert(word, root);
traverse(root,nullptr,0);
//cout<<"ans"<<" "<<ans;
int retans=ans;
ans=1;
return retans;
}