Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › STRING BASICS
Treat "bananaba" as the text and "naba" as the target. A candidate start is an index in the text where you try to place the target. The target has four characters, so a complete match requires text[i + 0] to equal target[0], text[i + 1] to equal target[1], text[i + 2] to equal target[2], and text[i + 3] to equal target[3]. Matching only the first few characters proves only that the alignment is still possible, not that the substring has been found.
At candidate start 2, the text characters are n, a, n, a. The first two comparisons succeed: text[2] equals target[0], and text[3] equals target[1]. The third comparison fails because text[4] is 'n' while target[2] is 'b'. The alignment "na" is therefore a partial match, not a complete match. At candidate start 4, the text characters are n, a, b, a, which match every character of the target.
Is candidate start 2 a complete match for "naba" in "bananaba"?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Here n is 8 and m is 4. A candidate start must leave room for four text characters, so the valid starts are 0 through n - m, which is 4. The search must therefore test starts 0, 1, 2, 3, and 4.
| CANDIDATE START | REMAINING TEXT INDICES | CAN FOUR CHARACTERS FIT? |
|---|---|---|
| 0 | 0-7 | Yes |
| 1 | 1-7 | Yes |
| 2 | 2-7 | Yes |
| 3 | 3-7 | Yes |
| 4 | 4-7 | Yes |
Start 4 is the boundary case: indices 4, 5, 6, and 7 provide exactly four characters for "naba". A start of 5 would need indices 5, 6, 7, and 8, but index 8 is outside the text. Testing only while i < n - m means i < 4, so it stops at start 3 and skips the valid match at start 4. The inclusive condition i <= n - m includes the final alignment.
Use an outer loop for candidate starts and an inner loop for target characters. The target index j begins at 0 for every new candidate. If a comparison fails, abandon that alignment and move to the next candidate start. Do not continue with the old j, because j describes progress inside one alignment and has no meaning for the next one.
| START | COMPARISONS | RESULT |
|---|---|---|
| 0 | text[0] = 'b' versus target[0] = 'n' | Mismatch at j = 0; next start is 1 with j reset to 0 |
| 1 | text[1] = 'a' versus target[0] = 'n' | Mismatch at j = 0; next start is 2 with j reset to 0 |
| 2 | text[2] = 'n' versus target[0] = 'n'; text[3] = 'a' versus target[1] = 'a'; text[4] = 'n' versus target[2] = 'b' | Mismatch at j = 2; next start is 3 with j reset to 0 |
| 3 | text[3] = 'a' versus target[0] = 'n' | Mismatch at j = 0; next start is 4 with j reset to 0 |
| 4 | text[4] = 'n' versus target[0] = 'n'; text[5] = 'a' versus target[1] = 'a'; text[6] = 'b' versus target[2] = 'b'; text[7] = 'a' versus target[3] = 'a' | All four comparisons match; j reaches 4 |
At start 2, j reaches 2 before the mismatch, but that value must not carry into start 3. Start 3 compares text[3] with target[0], not target[2]. At start 4, every comparison succeeds, so j advances from 0 to 4. Reaching j = m is the evidence that the entire target matched.
Fix the outer-loop condition so the search also tests candidate start 4.
for (int i = 0; i < n - m; i++) {Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A candidate succeeds only when the inner loop has compared all m target characters. In this example, start 4 succeeds when j reaches 4, which equals m, so the search returns 4. A partial match such as the two characters found at start 2 must not return its start, because the target still has unmatched characters.
for (int i = 0; i <= n - m; i++) {
int j = 0;
while (j < m && text[i + j] == target[j]) {
j++;
}
if (j == m) {
return i;
}
}
return -1;If every candidate reaches a mismatch before j becomes m, the outer loop ends and execution reaches return -1. That value means no complete alignment was found. For the running example, the search returns 4 because the candidate at start 4 passes all four comparisons, not because an earlier candidate matched a prefix.
There are n - m + 1 candidate starts, and each can compare up to m characters. The worst-case work is therefore O((n - m + 1) * m), commonly written O(n * m). The extra variables i and j use O(1) additional space, apart from the text and target already provided.