Introduction
The Naive Pattern Matching problem involves finding all occurrences of a pattern inside a text string.
Given:
- a string text
- a string pattern
the task is to:
- find all starting indices
where pattern
occurs inside text
This is the most basic string matching algorithm.
This problem helps in understanding:
- pattern matching
- substring comparison
- sequential traversal
- string searching
Example
Input:text = "AABAACAADAABAABA" pattern = "AABA"
Output: 0 9 12
Explanation:
Pattern occurs at:
index 0
index 9
index 12
Input:
text = "hello" pattern = "ll"
Output: 2
Explanation: Pattern starts at index 2.
1 <= text.length,pattern.length <= 10^5
text and pattern
contain English characters
Approach : Naive Pattern Matching
Explanation
The idea is very simple:
- Start pattern comparison
from every possible index - Compare characters one by one
- If all characters match:
- store index
- Continue traversal
This algorithm is called:
Naive Pattern Matchingbecause it checks every possible position directly.
Steps
- Traverse possible starting positions.
- Compare pattern characters.
- If mismatch occurs:
- move to next position
- If complete match found:
- store current index
- Continue traversal.
Dry Run
Input:text = "AABAACAADAABAABA"pattern = "AABA"
Check index 0:
AABA
Match found Store index 0
Check index 1: ABAA
Mismatch found
Check index 9: AABA Match found Store index 9
Check index 12:
AABA
Match found Store index 12
Final Result:
0 9 12
Naive Pattern Matching Code
Complexity Analysis
Time Complexity: O(n * m)Explanation:Pattern comparison is performed
for every possible position.
Space Complexity: O(1) Explanation:
No extra data structures are used
except result storage.
Edge Cases
- Pattern longer than text
- Pattern appears multiple times
- Pattern not present
- Pattern equals text
- Single character pattern
Why This Problem is Important
This problem helps in understanding:
- Basic pattern matching
- String traversal
- Sequential comparison
- Searching techniques
- Substring matching
It is the foundation for advanced algorithms like:
- KMP Algorithm
- Rabin Karp
- Z Algorithm
Real-World Applications
Pattern matching concepts are used in:
- Search engines
- Text editors
- DNA sequence analysis
- Plagiarism detection
- Data filtering systems
Common Mistakes
- Incorrect loop bounds
- Index out of range errors
- Missing multiple matches
- Wrong substring comparison
Interview Tips
Interviewers often expect:
- Proper traversal explanation
- Time complexity analysis
- Pattern matching understanding
Always explain why traversal stops at:
n - mwhere:
- n = text length
- m = pattern length
Related Questions
- Implement strStr()
- KMP Algorithm
- Rabin Karp
- Minimum Window Substring
- Longest Substring Without Repeating Characters
Final Takeaway
The Naive Pattern Matching problem is a fundamental string searching problem that teaches sequential substring comparison techniques. Understanding this problem builds a strong foundation for advanced string matching and pattern searching interview problems.