Intermediate Level
Implement the countPatternOccurrences method that counts how many times a pattern appears inside the given text.
The input contains two strings: text and pattern. Your task is to count how many times pattern appears inside text.
Overlapping matches must also be counted. For example, in aaaa, the pattern aa appears at positions 0, 1, and 2, so the answer is 3.
Check every possible starting position in text where the pattern can fit.
At each position, compare the substring of the same length as pattern. If it matches, increase the count. Move only one position at a time so overlapping occurrences are also included.
Pseudocode:
function countPatternOccurrences(text, pattern):
count = 0
textLength = length of text
patternLength = length of pattern
for i from 0 to textLength - patternLength:
if substring of text from i with patternLength == pattern:
count++
return count