From 5f76bae4c440db0c0f8bb77a9776c6a0903168d1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 21:54:39 +0000 Subject: [PATCH 1/4] Initial plan From bfae2270329305883c6d188b5f526f7535d49997 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 21:56:25 +0000 Subject: [PATCH 2/4] Add comprehensive comments to 0567-permutation-in-string.py solution Co-authored-by: Taophycc <166020709+Taophycc@users.noreply.github.com> --- .../0567-permutation-in-string.py | 27 ++++++++++++++++++ ...0567-permutation-in-string.cpython-312.pyc | Bin 0 -> 2288 bytes 2 files changed, 27 insertions(+) create mode 100644 0567-permutation-in-string/__pycache__/0567-permutation-in-string.cpython-312.pyc diff --git a/0567-permutation-in-string/0567-permutation-in-string.py b/0567-permutation-in-string/0567-permutation-in-string.py index 49fe52f..1d6c278 100644 --- a/0567-permutation-in-string/0567-permutation-in-string.py +++ b/0567-permutation-in-string/0567-permutation-in-string.py @@ -1,34 +1,61 @@ class Solution: def checkInclusion(self, s1: str, s2: str) -> bool: + """ + Check if s2 contains a permutation of s1 using sliding window approach. + + Time Complexity: O(n) where n is length of s2 + Space Complexity: O(1) - arrays of fixed size 26 + + Algorithm: + 1. Use frequency arrays to count character occurrences + 2. Use a sliding window of size len(s1) over s2 + 3. Track number of matching character frequencies between windows + 4. If all 26 letters match, we found a permutation + """ + # Edge case: if s1 is longer than s2, no permutation can exist in s2 if len(s1) > len(s2): return False + # Initialize frequency arrays for 26 lowercase letters s1Count, s2Count = [0]*26, [0]*26 + # Build frequency count for s1 and the first window of s2 for i in range(len(s1)): s1Count[ord(s1[i]) - ord('a')] += 1 s2Count[ord(s2[i]) - ord('a')] += 1 + # Count how many of the 26 letters have matching frequencies + # This optimization avoids comparing entire arrays repeatedly matches = 0 for i in range(26): matches += (1 if s1Count[i] == s2Count[i] else 0) + # Slide the window across s2 l = 0 for r in range(len(s1), len(s2)): + # If all 26 characters match, we found a permutation if matches == 26: return True + # Add the new character entering the window (right side) index = ord(s2[r]) - ord('a') s2Count[index] += 1 if s1Count[index] == s2Count[index]: + # This character now matches, increment matches matches += 1 elif s1Count[index] + 1 == s2Count[index]: + # This character was matching before, but now exceeds count matches -= 1 + # Remove the character leaving the window (left side) index = ord(s2[l]) - ord('a') s2Count[index] -= 1 if s1Count[index] == s2Count[index]: + # This character now matches after removal matches += 1 elif s1Count[index] - 1 == s2Count[index]: + # This character was matching before removal matches -= 1 l += 1 + + # Check the final window return matches == 26 diff --git a/0567-permutation-in-string/__pycache__/0567-permutation-in-string.cpython-312.pyc b/0567-permutation-in-string/__pycache__/0567-permutation-in-string.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5aeb990b082a216024ed0c52d613c9633901a2e9 GIT binary patch literal 2288 zcmcIm%}*Og6rbJo+L)S9V!)&VGEI8GQGwV5sAw9Ak`JY-s4A!;AywA0JH{*4yJU7< zV`N7?P{|YyAdoOOqo9f?mmJf-p_f8>K&u{*dgA5+Ql*^wX6;=AZK{5#Bkj(cdGGz+ z@4b1R^$$%=jR?lC-z=z$L4yQ({hx`t61ZL(5pIq1(I}gC}nLDX}tCmxUS< zn;0rvpHv|7CbBrT$wr=gUX#!4@$TO2U4$r?;}m{|Q-lQ(p}JJ?6$HzRamL$2bW!f2 zFF!@7Am#-N5UL~3<;5!?x;Ax<;&b;FptAG)JkRVDaiIwzS&;eLu*&=D_IdulAgzr1 zAg6-Ao{1v3@qu@%!`q)1sxk7u>O9*6b>S6JhsrQs1^-IBgA=IZ{8>fL?0GeIdjX>D z08&HVuLxCq4(0sk@@)7!6)OaRW$+Dk6Y@cZ!7SM9#PLH!r@95d9)?UE4eVUB6CJw< z%k~_rz`tSzINoXg6IRd@=h&ZNl@HYN2&{bPLv9@>P&6PNL6bM)+89d_c^0clY{s!{ z=$54FCYJCVq3MhzvELFlKpDdsQ`Kj%si_K^v#PEbSuD-XQA3hbeIA}COsZ*u$Bgux zM&?y3H-bOw(W5woXM<{>O4`fsV)#4 zAMzYMVN{zjsA{FsBi>f54}W12oTTJ&hUjw61+xrDIip)xPDxafEkdy&%Na_+h?t&x zyy`ApOrF(+v4BK+Odx4|1psC}mw`S!39z$R&!ndrb`qy0OHQ%+)g!vBRAS<3Vr2=@ zYt^kYxYdUrCb6VxkTHa_fT>xb<5#A@Tkv->ediVIlVN@jef%{# z?L?vGB3&Ndm?&Ldf3*Kr=RvsRu(PW)P@-Ey<QZao7+p*zAbD|0`azvKaecm18%GLmd8s#_3HBYL38_RX6Z>evfH-Z zz2Dq__;#ceTMMlP4q7@&ZEM}-clTPZm$SQ>?I-&!gEj3ctIGceeh_L}ZCSd%G+OD0 zlq$CnaJsfs__3k$1Wpi*vu}1Hp-X9!NZ5^uMA}d?8k0kb#N&*l)ieS$kL{Z_42`mX z56=)~*DQr=6H)fnbzbb#ZFa(I5mopRv8x=eAeeTqJJXBzPT8vJ$b815T-Em~1Mu)( eH{7n0xI-~09MEPE;KVrYH~vGe>lneyarhnGAudq> literal 0 HcmV?d00001 From be96b5475ff77f3f77f64de509542dfa80dd07ba Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 21:56:40 +0000 Subject: [PATCH 3/4] Add __pycache__ to .gitignore and remove from repository Co-authored-by: Taophycc <166020709+Taophycc@users.noreply.github.com> --- .gitignore | 5 +++++ .../0567-permutation-in-string.cpython-312.pyc | Bin 2288 -> 0 bytes 2 files changed, 5 insertions(+) delete mode 100644 0567-permutation-in-string/__pycache__/0567-permutation-in-string.cpython-312.pyc diff --git a/.gitignore b/.gitignore index f2e0521..8f43a03 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ # macOS system files .DS_Store + +# Python cache files +__pycache__/ +*.py[cod] +*$py.class diff --git a/0567-permutation-in-string/__pycache__/0567-permutation-in-string.cpython-312.pyc b/0567-permutation-in-string/__pycache__/0567-permutation-in-string.cpython-312.pyc deleted file mode 100644 index 5aeb990b082a216024ed0c52d613c9633901a2e9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2288 zcmcIm%}*Og6rbJo+L)S9V!)&VGEI8GQGwV5sAw9Ak`JY-s4A!;AywA0JH{*4yJU7< zV`N7?P{|YyAdoOOqo9f?mmJf-p_f8>K&u{*dgA5+Ql*^wX6;=AZK{5#Bkj(cdGGz+ z@4b1R^$$%=jR?lC-z=z$L4yQ({hx`t61ZL(5pIq1(I}gC}nLDX}tCmxUS< zn;0rvpHv|7CbBrT$wr=gUX#!4@$TO2U4$r?;}m{|Q-lQ(p}JJ?6$HzRamL$2bW!f2 zFF!@7Am#-N5UL~3<;5!?x;Ax<;&b;FptAG)JkRVDaiIwzS&;eLu*&=D_IdulAgzr1 zAg6-Ao{1v3@qu@%!`q)1sxk7u>O9*6b>S6JhsrQs1^-IBgA=IZ{8>fL?0GeIdjX>D z08&HVuLxCq4(0sk@@)7!6)OaRW$+Dk6Y@cZ!7SM9#PLH!r@95d9)?UE4eVUB6CJw< z%k~_rz`tSzINoXg6IRd@=h&ZNl@HYN2&{bPLv9@>P&6PNL6bM)+89d_c^0clY{s!{ z=$54FCYJCVq3MhzvELFlKpDdsQ`Kj%si_K^v#PEbSuD-XQA3hbeIA}COsZ*u$Bgux zM&?y3H-bOw(W5woXM<{>O4`fsV)#4 zAMzYMVN{zjsA{FsBi>f54}W12oTTJ&hUjw61+xrDIip)xPDxafEkdy&%Na_+h?t&x zyy`ApOrF(+v4BK+Odx4|1psC}mw`S!39z$R&!ndrb`qy0OHQ%+)g!vBRAS<3Vr2=@ zYt^kYxYdUrCb6VxkTHa_fT>xb<5#A@Tkv->ediVIlVN@jef%{# z?L?vGB3&Ndm?&Ldf3*Kr=RvsRu(PW)P@-Ey<QZao7+p*zAbD|0`azvKaecm18%GLmd8s#_3HBYL38_RX6Z>evfH-Z zz2Dq__;#ceTMMlP4q7@&ZEM}-clTPZm$SQ>?I-&!gEj3ctIGceeh_L}ZCSd%G+OD0 zlq$CnaJsfs__3k$1Wpi*vu}1Hp-X9!NZ5^uMA}d?8k0kb#N&*l)ieS$kL{Z_42`mX z56=)~*DQr=6H)fnbzbb#ZFa(I5mopRv8x=eAeeTqJJXBzPT8vJ$b815T-Em~1Mu)( eH{7n0xI-~09MEPE;KVrYH~vGe>lneyarhnGAudq> From c3011ffb55558cc0c0aae3cd9d6cef1f22b27e12 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 21:57:36 +0000 Subject: [PATCH 4/4] Fix time complexity comment to be more accurate Co-authored-by: Taophycc <166020709+Taophycc@users.noreply.github.com> --- 0567-permutation-in-string/0567-permutation-in-string.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0567-permutation-in-string/0567-permutation-in-string.py b/0567-permutation-in-string/0567-permutation-in-string.py index 1d6c278..d9772ed 100644 --- a/0567-permutation-in-string/0567-permutation-in-string.py +++ b/0567-permutation-in-string/0567-permutation-in-string.py @@ -3,7 +3,7 @@ def checkInclusion(self, s1: str, s2: str) -> bool: """ Check if s2 contains a permutation of s1 using sliding window approach. - Time Complexity: O(n) where n is length of s2 + Time Complexity: O(len(s1) + len(s2)) for initial setup and sliding window Space Complexity: O(1) - arrays of fixed size 26 Algorithm: