Overlapping occurrences in AP Computer Science A

Overlapping occurrences are multiple matches of a substring that share characters, so 'aa' appears 3 times in 'aaaabb' (at indexes 0, 1, and 2). In AP CSA, counting them means advancing your loop index by 1 after every match instead of jumping past the whole matched substring.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What are overlapping occurrences?

Overlapping occurrences happen when copies of a substring share characters with each other. Take the string "aaaabb" and the target "aa". You can find "aa" starting at index 0, index 1, and index 2. That's 3 occurrences, even though they pile on top of each other. If you required the matches to be separate (non-overlapping), you'd only count 2.

In Java, the whole concept comes down to one line of code. When you write a substring-counting loop, you check text.substring(i, i + target.length()).equals(target) at each position. After a match, do you move i forward by 1, or by target.length()? Moving by 1 lets the next check start inside the match you just found, so you count overlapping occurrences. Jumping ahead by target.length() skips past the matched characters, so you only count non-overlapping ones. Same loop, same condition, totally different answer.

Why overlapping occurrences matter in AP® Computer Science A

This lives in Topic 2.10 (Developing Algorithms Using Strings) in Unit 2, under learning objective 2.10.A. EK 2.10.A.1 says you need standard string algorithms that determine the number of substrings meeting specific criteria, and substring counting is the classic example. Overlapping vs. non-overlapping is exactly the kind of detail the exam uses to separate people who memorized a counting loop from people who actually trace code. Two methods can look nearly identical, and the only difference is whether the index advances by 1 or by the target's length after a match. If you can't spot that, you'll pick the wrong answer count on an MCQ or write a method that fails edge cases on an FRQ.

How overlapping occurrences connect across the course

Developing Algorithms Using Strings (Unit 2)

Substring counting is one of the standard algorithms EK 2.10.A.1 names directly. Overlapping occurrences is the twist that gets layered on top of the basic counting pattern, so master the plain count-the-substring loop first, then ask yourself how the match-handling line changes the result.

replaceNthOccurrence (Unit 2)

Methods like replaceNthOccurrence have to find occurrences before they can replace one, which means they face the same overlapping question. If the first occurrence overlaps the second, replacing the first one can destroy the second. Counting and replacing are two sides of the same find-the-substring logic.

While loops and loop bounds (Unit 2)

Every occurrence-counting loop runs while i <= text.length() - target.length(), and that bound is its own trap. Using < instead of <= silently misses a match at the very end of the string. Overlapping-occurrence questions almost always test the loop bound and the index increment together.

Sentinel value (Unit 2)

Some string searches loop until indexOf returns -1, which acts as a sentinel meaning no more matches. Where you restart the search after each hit (one past the match start, or past the whole match) is the indexOf version of the same overlapping vs. non-overlapping choice.

Are overlapping occurrences on the AP® Computer Science A exam?

Expect this in multiple-choice code-tracing questions. A typical stem gives you a counting method like countSub(String text, String target) and asks what it returns for a string with overlapping matches, or shows a version with missing code and asks which option correctly counts non-overlapping occurrences. The answer hinges on two things you should check immediately. First, the loop bound (i <= text.length() - target.length() catches the final position; < misses it). Second, the increment after a match (i++ counts overlaps, i += target.length() doesn't). On the FRQ side, the 2021 FRQ Q1 (WordMatch) asked for methods comparing strings against a secret string position by position, which is the same substring-and-compare machinery. When you write any string-counting method on an FRQ, decide up front whether the spec wants overlapping matches counted, and make your increment match that decision.

Overlapping occurrences vs Non-overlapping occurrences

Overlapping occurrences can share characters; non-overlapping occurrences can't. In "aaaabb", the target "aa" has 3 overlapping occurrences (indexes 0, 1, 2) but only 2 non-overlapping ones (indexes 0 and 2). In code, i++ after a match counts overlapping occurrences, while i += target.length() counts non-overlapping ones. Exam questions love showing you one version and seeing if you assume it's the other.

Key things to remember about overlapping occurrences

  • Overlapping occurrences are substring matches that share characters, so 'aa' appears 3 times in 'aaaabb' even though the matches pile on top of each other.

  • Incrementing the loop index by 1 after a match counts overlapping occurrences, while incrementing by target.length() counts only non-overlapping ones.

  • The standard counting loop runs while i <= text.length() - target.length(), and using < instead of <= misses a match at the end of the string.

  • This concept supports learning objective 2.10.A and EK 2.10.A.1, which require algorithms that count substrings meeting specific criteria.

  • When you trace a counting method on a multiple-choice question, check the increment after a match before you start counting anything.

Frequently asked questions about overlapping occurrences

What are overlapping occurrences in AP Computer Science A?

They're multiple matches of a substring that share characters within a string. For example, 'aa' occurs 3 times in 'aaaabb' because the matches at indexes 0, 1, and 2 overlap each other.

Does i++ count overlapping or non-overlapping occurrences?

Overlapping. Moving the index forward by just 1 after a match lets the next check start inside the previous match. To count non-overlapping occurrences, jump ahead with i += target.length() instead.

How many times does 'aa' appear in 'aaaabb'?

3 times if you count overlapping occurrences (starting at indexes 0, 1, and 2), but only 2 times if you count non-overlapping occurrences (indexes 0 and 2). This exact distinction shows up in AP CSA multiple-choice questions.

What's the difference between overlapping and non-overlapping occurrences?

Overlapping occurrences can share characters between matches; non-overlapping ones can't. In code the only difference is one line, advancing the index by 1 versus by the target's full length after each match.

Is overlapping occurrence counting on the AP CSA exam?

Yes, it falls under Topic 2.10 and EK 2.10.A.1, which covers determining the number of substrings meeting specific criteria. It typically appears as a code-tracing MCQ, and the substring-comparison skill behind it showed up in the 2021 FRQ Q1 (WordMatch).