Tries Prefix trees
427. Number of Distinct Substrings in a String
Medium
You are given a string s. Return the number of different substrings of s.
A substring is a block of letters that sit next to each other in s, with at least one letter. Substrings
that hold the same letters count once, however many times and wherever they appear in s.
Example 1
Input: s = "cocoa"
Output:
12Explanation: By length, the different substrings are c, o, a; co, oc, oa; coc, oco, coa; coco,
ocoa; and cocoa. c, o and co each appear twice but count once.
Example 2
Input: s = "x"
Output:
1Example 3
Input: s = "aaaa"
Output:
4Explanation: The different substrings are a, aa, aaa and aaaa. The other six substrings repeat one of these.
Check your understanding
Work out the answer for this input before you write any code.
s = "abab"
Constraints
1 ≤ s.length ≤ 1000sholds lowercase English letters only.
Clarifying questions
What you would ask an interviewer before writing any code.