---
title: "Overlapping Occurrences — AP CSA Definition & Examples"
description: "Overlapping occurrences are substring matches that share characters, like 'aa' appearing 3 times in 'aaaabb'. Master the i++ vs i += length distinction for AP CSA."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/overlapping-occurrences"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 2"
---

# Overlapping Occurrences — AP CSA Definition & Examples

## Definition

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.

## What It Is

Overlapping occurrences happen when copies of a [substring](/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW "fv-autolink") share characters with each other. Take the string `"aaaabb"` and the target `"aa"`. You can find `"aa"` starting at [index](/ap-comp-sci-a/key-terms/index "fv-autolink") 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](/ap-comp-sci-a/key-terms/loop "fv-autolink"), 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 It Matters

This lives in Topic 2.10 (Developing Algorithms Using Strings) in [Unit 2](/ap-comp-sci-a/unit-2 "fv-autolink"), 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](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz "fv-autolink") 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.

## Connections

### Developing Algorithms Using Strings (Unit 2)

Substring counting is one of the standard [algorithms](/ap-comp-sci-a/key-terms/algorithm "fv-autolink") 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)](/ap-comp-sci-a/key-terms/replacenthoccurrence)

Methods like [replaceNthOccurrence](/ap-comp-sci-a/key-terms/replacenthoccurrence "fv-autolink") 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)](/ap-comp-sci-a/key-terms/sentinel-value)

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](/ap-comp-sci-a/key-terms/indexof "fv-autolink") version of the same overlapping vs. non-overlapping choice.

## On the AP 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 Takeaways

- 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.

## FAQs

### 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).

## Related Study Guides

- [2.10 Developing Algorithms Using Strings](/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/overlapping-occurrences#resource","name":"Overlapping Occurrences — AP CSA Definition & Examples","url":"https://fiveable.me/ap-comp-sci-a/key-terms/overlapping-occurrences","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/overlapping-occurrences#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T05:27:18.528Z","isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/overlapping-occurrences#term","name":"overlapping occurrences","description":"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.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/overlapping-occurrences","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What are overlapping occurrences in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Does i++ count overlapping or non-overlapping occurrences?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"How many times does 'aa' appear in 'aaaabb'?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"What's the difference between overlapping and non-overlapping occurrences?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Is overlapping occurrence counting on the AP CSA exam?","acceptedAnswer":{"@type":"Answer","text":"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)."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Unit 2","item":"https://fiveable.me/ap-comp-sci-a/unit-2"},{"@type":"ListItem","position":4,"name":"overlapping occurrences"}]}]}
```
