Intermediate Level
Implement the permutationInStringCheck method that checks whether the text contains any permutation of the pattern.
The input contains two strings: pattern and text. Your task is to check whether any permutation of pattern appears as a continuous substring inside text.
A permutation uses exactly the same characters with the same frequencies, but the order can be different.
For example, ab appears as the permutation ba inside eidbaooo, so the answer is true.
Use a sliding window whose length is the same as pattern.
Build the character frequency count for pattern. Then move a same-sized window across text and maintain the character count of the current window.
If both frequency counts match at any point, the current window is a permutation of pattern.
Pseudocode:
function permutationInStringCheck(pattern, text):
m = length of pattern
n = length of text
if m > n:
return false
patternCount = frequency count of pattern
windowCount = frequency count of first m characters of text
if windowCount == patternCount:
return true
for right from m to n - 1:
add text[right] to windowCount
leftChar = text[right - m]
remove one count of leftChar from windowCount
if windowCount == patternCount:
return true
return false