oneby7

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: 12

Explanation: 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: 1

Example 3

Input: s = "aaaa"

Output: 4

Explanation: 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 ≤ 1000
  • s holds lowercase English letters only.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.