Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7036449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:25:32+00:00 2026-05-28T01:25:32+00:00

I need to get all the unique substring of a string. I have stored

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:25:32+00:00Added an answer on May 28, 2026 at 1:25 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need get all items these have no categories int? categoryId = null; var
I need to get all substrings from string. For ex: StringParser.GetSubstrings([start]aaaaaa[end] wwwww [start]cccccc[end], [start],
I need to get the unique USB ID (not a Volume serial number )
I have a unique set of processes that all need to be automated. We
We need to get all the instances of objects that implement a given interface
I need to get all dlls in my application root directory. What is the
I need to get all the objects whose id matches a specific pattern .
I need to get all controls inside a specific RowDefinition/ColumnDefinition without iterating through all
I need to get all characters between '(' and ')' chars. var str =
I am building a web app where I need to get all the images

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.