Find All Substrings of the String aabaabb
At a glance
- 1. Use Contiguous Blocks
A substring is a consecutive run of characters, not a selection that skips positions.
- 2. List Each Length
Slide a window of each length across aabaabb and record the resulting strings.
- 3. Remove Duplicates
Keep one copy of each distinct result, grouped by length.
- 4. Check the Count
There are 28 substring occurrences in total and 20 distinct nonempty substrings.
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
| Length | Distinct substrings |
|---|---|
| 1 | a, b |
| 2 | aa, ab, ba, bb |
| 3 | aab, aba, abb, baa |
| 4 | aaba, abaa, aabb, baab |
| 5 | aabaa, abaab, baabb |
| 6 | aabaab, abaabb |
| 7 | aabaabb |
Adding the entries by row gives 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 has one substring for each choice of a starting position and an ending position at or after it. The number is
For , this is . 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:
aaais not a substring ofaabaabb, because its threeacharacters are not consecutive. A selection that can skip positions is usually called a subsequence. - Counting occurrences as distinct values: The block
aabappears at positions 1–3 and 4–6, but it is one distinct substring value. - Forgetting short or full-length blocks: The single-character blocks
aandbcount, as does the full stringaabaabb. - 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.