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!
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at Adobe

Q. Unbounded Knapsack Problem Statement Given ‘N’ items, each with a specific profi...read more
Q. Minimum Cost to Make String Valid Given a string containing only '{' and '}', de...read more
Q. Digit Count In Range Problem Statement Given an integer K, and two numbers A and...read more
Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits