Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › STRING BASICS
In the string "abbaccca", the character 'a' appears at indices 0, 3, and 7, but those positions are separated. They do not form a consecutive group. The two 'b' characters at indices 1 and 2 are adjacent, and the three 'c' characters at indices 4, 5, and 6 are adjacent. The decision depends on neighboring positions, not on how many times a character appears in the whole string.
To check adjacency, compare each character with the character immediately before it. A match means the current character continues a run. A mismatch means the current character belongs to a new run, even if the same character appeared earlier somewhere else.
Do the three occurrences of 'a' in "abbaccca" prove that three identical characters are consecutive?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Start at index 1 because index 0 has no character before it to compare against. Keep a current streak length, beginning at 1 for the first character. At each index i, compare s[i] with s[i - 1]. If they are equal, increase the streak. If they differ, reset the streak to 1 because s[i] starts a new streak.
int streak = 1;
for (int i = 1; i < s.length(); i++) {
if (s[i] == s[i - 1]) {
streak++;
} else {
streak = 1;
}
}For "abbaccca", index 1 contains 'b' while index 0 contains 'a', so the streak is 1. Index 2 contains another 'b', so the streak becomes 2. Index 3 contains 'a' instead of 'b', so the streak resets to 1. The same reset happens at index 4, then the three 'c' characters grow the streak from 1 to 2 to 3.
Use one flag for reaching a streak of 2 and another for reaching a streak of 3. After updating the current streak, set hasTwo when streak is at least 2 and set hasThree when streak is at least 3. Once a flag becomes true, do not reset it when a later character starts a new streak.
if (streak >= 2) {
hasTwo = true;
}
if (streak >= 3) {
hasThree = true;
}The 'b' streak at indices 1-2 changes hasTwo to true. The later 'c' streak reaches length 3 at index 6, so it changes hasThree to true. A streak of three also contains an adjacent pair, which means hasTwo must already be true when hasThree becomes true. When index 7 contains 'a', the current streak resets to 1, but both flags stay true because their milestones were already reached.
When s[i] differs from s[i - 1], supply the streak update.
if (s[i] != s[i - 1]) {
streak = ___;
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
For a string of length 8 with indices 0-7, the loop visits i from 1 through 7. At every visit, s[i] is valid because i is at most 7, and s[i - 1] is valid because i is at least 1. The equivalent loop condition is i < 8, or more generally i < s.length(). Starting at 0 would try to read s[-1] on the first comparison.
| INDEX | CHARACTER | STREAK | HASTWO | HASTHREE |
|---|---|---|---|---|
| 0 | a | 1 | false | false |
| 1 | b | 1 | false | false |
| 2 | b | 2 | true | false |
| 3 | a | 1 | true | false |
| 4 | c | 1 | true | false |
| 5 | c | 2 | true | false |
| 6 | c | 3 | true | true |
| 7 | a | 1 | true | true |
bool hasTwo = false;
bool hasThree = false;
int streak = 1;
for (int i = 1; i < s.length(); i++) {
if (s[i] == s[i - 1]) {
streak++;
} else {
streak = 1;
}
if (streak >= 2) hasTwo = true;
if (streak >= 3) hasThree = true;
}After index 7, the final streak is 1, hasTwo is true, and hasThree is true. The scan takes one pass through the string, so its time is O(n), where n is the string length. It stores only the current streak and two flags, so its extra space is O(1), regardless of how long the string becomes.