ADVANCED DATA STRUCTURES PROGRAM • LEVEL 11 — STRING PATTERN MATCHING
Find Overlapping Matches with Naive Search
Learn how to find overlapping matches with naive search using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
Matches: 0 2 4
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
#include <string.h>
int main(void)
{
const char text[] = "ABABABA", pattern[] = "ABA";
int text_length = (int) strlen(text), pattern_length = (int) strlen(pattern);
printf("Matches:");
for (int start = 0; start <= text_length - pattern_length; start++) {
int offset = 0;
while (offset < pattern_length && text[start + offset] == pattern[offset]) offset++;
if (offset == pattern_length) printf(" %d", start);
}
putchar('\n'); return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Matches: 0 2 4
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Try the pattern at every valid start, including positions that overlap a previous match.
- Display the computed result.
Try the pattern at every valid start, including positions that overlap a previous match.
EFFICIENCY
Time and space complexity
Time complexity
O(nm)
Auxiliary space
O(pattern length)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
