This repository contains solutions to the "Check Inclusion of Permutation" problem implemented in multiple languages: C++, Java, JavaScript, Python, and Go. Each language implements a sliding window approach to solve the problem efficiently.
Given two strings s1
and s2
, the task is to check if a permutation of s1
is present as a substring in s2
. A sliding window technique is used to compare character frequency counts of s1
with substrings of s2
.
- Condition: If the length of
s1
is greater than the length ofs2
, immediately returnfalse
. This is becauses2
cannot contain any permutation ofs1
if it's shorter.
-
Count Frequency: Initialize two frequency count arrays (or lists) of size 26 (for 26 letters in the alphabet). One array counts the frequency of characters in
s1
, and the other does the same for the first window (substring of the same length ass1
) ins2
.Example:
-
For each character in
s1
, update its count ins1Count
. -
For each character in the first window of
s2
, update its count ins2Count
.
- Slide Window: Slide the window across
s2
and update the character frequency counts as you go. At each step, compare the counts of the current window ins2
with the counts ofs1
. - Compare Counts: If the two frequency arrays match, return
true
because a permutation ofs1
is found ins2
. - Window Update:
- Decrement the count of the character that is sliding out of the window (the leftmost character).
- Increment the count of the new character that is entering the window (the rightmost character).
- Last Window: After sliding through the entire
s2
, compare the frequency counts one last time for the final window ins2
. If they match, returntrue
; otherwise, returnfalse
.
- Initial Checks: Compare the length of
s1
ands2
. - Frequency Count Setup: Use two
vector<int>
arrays to track the frequency of each character ins1
and the first window ofs2
. - Sliding the Window: Slide the window across
s2
by adjusting the frequency counts of characters entering and leaving the window. - Final Check: Compare the counts of the final window with
s1Count
.
- Initial Checks: Check if
s1
is longer thans2
. Returnfalse
if it is. - Frequency Count Setup: Initialize two
int[]
arrays for frequency counts. - Sliding the Window: Move the window across
s2
and update counts using the helper methodmatches
to compares1Count
ands2Count
. - Final Check: After sliding through all possible windows, compare the last window's count.
- Initial Checks: If the length of
s1
is larger thans2
, returnfalse
. - Frequency Count Setup: Create two arrays of size 26 for
s1Count
ands2Count
. - Sliding the Window: Update the frequency counts as the window moves across
s2
. Use a helper functionmatches
to compare the two arrays. - Final Check: After sliding through all windows, ensure the last window is checked.
- Initial Checks: Verify if
s1
is longer thans2
. If so, returnfalse
. - Frequency Count Setup: Create two lists of size 26 for character frequency counts.
- Sliding the Window: Adjust the counts for each new character entering and leaving the sliding window.
- Final Check: Perform the last check for the final window after sliding across
s2
.
- Initial Checks: If
s1
is longer thans2
, returnfalse
. - Frequency Count Setup: Create two slices (arrays) of size 26 for
s1Count
ands2Count
. - Sliding the Window: Update character counts by decrementing the count for the character that leaves the window and incrementing for the one that enters.
- Final Check: After sliding through
s2
, compare the frequency counts for the last window.
This step-by-step explanation follows a uniform structure in all the languages, highlighting how the sliding window technique is applied to solve the problem efficiently.