I need to get all the unique substring of a string. I have stored the string into a trie but I am not able to figure out how can i used the same to print all the unique substring
for example
string aab all unique substrings are {"a", "aa", "aab", "ab", "b"}
here is my code for trie
#include <iostream>
#include <map>
#include <string>
#include <stack>
struct trie_node_t {
typedef std::map<char, trie_node_t *> child_node_t;
child_node_t m_childMap;
trie_node_t() :m_childMap(std::map<char, trie_node_t*>()) {}
void insert( std::string& word ) {
trie_node_t *pNode = this;
for ( std::string::const_iterator itr = word.begin(); itr != word.end(); ++itr) {
char letter = *itr;
if ( pNode->m_childMap.find(letter) == pNode->m_childMap.end()){
pNode->m_childMap[letter] = new trie_node_t();
}
pNode = pNode->m_childMap[letter];
}
}
void print() {
}
};
int main ( int argc, char **argv ) {
trie_node_t trie;
trie.insert(std::string("aab"));
trie.print();
}
How do i implement print function which will print all the unique substring.
I am looking for Linear time approach
Since I have built a trie, is there a any way I can iterate over and whenever I visit any node I can print it as a unique string.
First, build a suffix tree. This represents all suffixes of the string and can be done in linear time. Since every substring is a prefix of a suffix, now you need to enumerate the prefixes of the suffixes.
Fortunately if two suffixes share a common prefix, the prefix will be on a single common path from root, so there’s a 1-1 mapping between paths from root(*) in the tree and unique suffixes.
Therefore it is sufficient to iterate over all paths from root in the suffix tree to produce all the unique substrings.
(*) The paths in the suffix tree are compressed, i.e. an edge might represent several characters. You need to uncompress the paths to produce all the substrings, i.e. treat compressed edges as multi-node paths.