Count Distinct Substrings
You are provided with a string S
. Your task is to determine and return the number of distinct substrings, including the empty substring, of this given string. Implement the solution using a trie data structure.
Input:
First line contains an integer T
representing the number of test cases.
For each test case, a single line contains the string S
.
Output:
Output consists of T lines, each containing an integer result for each test case, representing the number of distinct substrings of string S
.
Example:
Input:
2
ab
abc
Output:
4
7
Explanation:
For 'ab', distinct substrings are: "", "a", "b", "ab".
For 'abc', distinct substrings are: "", "a", "b", "c", "ab", "bc", "abc".
Constraints:
1 <= T <= 5
1 <= |S| <= 10^3
S
consists only of lowercase English letters.
Note:
No need to handle printing; this has been managed already. Focus purely on implementing the specified function.

AnswerBot
4mo
Count distinct substrings of a given string using trie data structure.
Implement a trie data structure to store all substrings of the given string.
Count the number of nodes in the trie to get the disti...read more
Help your peers!
Add answer anonymously...
Adobe Software Developer Intern interview questions & answers
A Software Developer Intern was asked Q. Unbounded Knapsack Problem Statement Given ‘N’ items, each with a specific profi...read more
A Software Developer Intern was asked Q. Minimum Cost to Make String Valid Given a string containing only '{' and '}', de...read more
A Software Developer Intern was asked Q. Digit Count In Range Problem Statement Given an integer K, and two numbers A and...read more
Popular interview questions of Software Developer Intern
A Software Developer Intern was asked Q1. Unbounded Knapsack Problem Statement Given ‘N’ items, each with a specific profi...read more
A Software Developer Intern was asked Q2. Minimum Cost to Make String Valid Given a string containing only '{' and '}', de...read more
A Software Developer Intern was asked Q3. Digit Count In Range Problem Statement Given an integer K, and two numbers A and...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

