Longest repeated substring
-
05-11-2019 - |
Question
I am trying to solve a problem- Longest repeated substring in a string. Firstly, I built a suffix tree that takes O(n) time and then I traversed the suffix tree to find the deepest internal node. I am not sure whether traversing in a suffix tree would be O(n) or not?
These are the properties of a suffix tree-
- A suffix tree will contain n leaves which are numbered from 1 to n.
- Each internal node (except root) should have at least 2 children.
If each internal node can have more than 2 children,How can I say that number of nodes in a suffix tree is O(n)? So, How will the complexity be O(n)?
Here is the code that I have written for traversal. I am aware that this platform is language independent so I have added the relevant comments along with my code.
struct StNode
{
string str;
StNode* child[256];
};
void lrs(StNode* t,string s,string &res)
{
if(leafNode(t)){ //leafNode means no child
string str=t->str;
s.erase(s.length()-str.length()); //erasing the leafNode's string
int k=s.length();
if(k>res.length()) res=s;
return;
}
for(int i=0;i<256;i++){ //traversing through all childs
if(t->child[i]!=NULL){
string str=t->child[i]->str;
lrs(t->child[i],s+str,res);
}
}
}
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with cs.stackexchange