Ma · Mathematics

Find All Substrings of the String aabaabb

Published By the 9to5Science editorial team

At a glance

  1. 1. Use Contiguous Blocks

    A substring is a consecutive run of characters, not a selection that skips positions.

  2. 2. List Each Length

    Slide a window of each length across aabaabb and record the resulting strings.

  3. 3. Remove Duplicates

    Keep one copy of each distinct result, grouped by length.

  4. 4. Check the Count

    There are 28 substring occurrences in total and 20 distinct nonempty substrings.

Advertisement

A substring is a consecutive block of characters taken from a string. For aabaabb, some substrings occur more than once, so the number of distinct substrings is smaller than the number of possible starting-and-ending position pairs.

There are 20 distinct nonempty substrings. The complete list is grouped by length below, so you can check each possible block size in turn.

All distinct substrings

LengthDistinct substrings
1a, b
2aa, ab, ba, bb
3aab, aba, abb, baa
4aaba, abaa, aabb, baab
5aabaa, abaab, baabb
6aabaab, abaabb
7aabaabb

Adding the entries by row gives 2+4+4+4+3+2+1=202+4+4+4+3+2+1=20 distinct nonempty substrings.

How to enumerate them

Write the string with its positions shown:

position:  1 2 3 4 5 6 7
character: a a b a a b b

For a fixed length, move a window from left to right, one character at a time. For example, the length-three windows are:

aab, aba, baa, aab, abb

The first and fourth windows are both aab, so this length contributes four distinct substrings, even though it has five occurrences. Repeat the process for each length from one through seven, removing any repeated result within that length or across the list. A substring cannot have two different lengths, so duplicates only need checking among windows of the same length.

For length four, the windows are aaba, abaa, baab, and aabb. For length five, they are aabaa, abaab, and baabb. The length-six windows are aabaab and abaabb; the only length-seven window is the entire string.

Why there are 28 occurrences

If occurrences at different positions are counted separately, a string of length nn has one substring for each choice of a starting position and an ending position at or after it. The number is

1+2+⋯+n=n(n+1)2.1+2+\cdots+n=\frac{n(n+1)}{2}.

For n=7n=7, this is 7⋅8/2=287\cdot8/2=28. This count includes repetitions: the two appearances of aab, for instance, are separate occurrences. It does not mean there are 28 different substring values. After removing duplicate values, the list above has 20.

Common mistakes

  • Counting nonconsecutive selections: aaa is not a substring of aabaabb, because its three a characters are not consecutive. A selection that can skip positions is usually called a subsequence.
  • Counting occurrences as distinct values: The block aab appears at positions 1–3 and 4–6, but it is one distinct substring value.
  • Forgetting short or full-length blocks: The single-character blocks a and b count, as does the full string aabaabb.
  • Adding the empty string without saying so: Some formal definitions include the empty string as a substring. Under that convention, the total of distinct substrings is 21; the list above uses the common convention of listing only nonempty substrings.

If the intended task is to find subsequences instead, the answer changes: a subsequence may skip characters while preserving their order. The word “substring” conventionally means a contiguous block, which is the interpretation used here.

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.