From d1af580e82608aed5ef65c2b45880ec4c45495e0 Mon Sep 17 00:00:00 2001 From: manas-1110 <125336538+manas-1110@users.noreply.github.com> Date: Sat, 5 Oct 2024 14:19:11 +0530 Subject: [PATCH 1/4] Add files via upload --- strings/Permutation_in_String.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 strings/Permutation_in_String.py diff --git a/strings/Permutation_in_String.py b/strings/Permutation_in_String.py new file mode 100644 index 000000000000..a8854cdb7c43 --- /dev/null +++ b/strings/Permutation_in_String.py @@ -0,0 +1,29 @@ +from collections import Counter + +class Solution: + def checkInclusion(self, s1: str, s2: str) -> bool: + n1, n2 = len(s1), len(s2) + if n2 < n1: + return False + freq1, freq2 = Counter(s1), Counter(s2[0:n1]) + if freq1 == freq2: + return True + l, r = 1, n1 + while r < n2: + freq2[s2[l-1]] -= 1 + if freq2[s2[l-1]] == 0: + del freq2[s2[l-1]] + freq2[s2[r]] += 1 + if freq1 == freq2: + return True + r += 1 + l += 1 + return False + +if __name__ == "__main__": + s1 = "ab" #update s1 + s2 = "eidbaooo" #update s2 + + sol = Solution() + result = sol.checkInclusion(s1, s2) + print(result) \ No newline at end of file From 6136c6699246f359ccba67ae2658539cdab6684d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Oct 2024 08:51:01 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/Permutation_in_String.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/strings/Permutation_in_String.py b/strings/Permutation_in_String.py index a8854cdb7c43..d3982cebdd46 100644 --- a/strings/Permutation_in_String.py +++ b/strings/Permutation_in_String.py @@ -1,5 +1,6 @@ from collections import Counter + class Solution: def checkInclusion(self, s1: str, s2: str) -> bool: n1, n2 = len(s1), len(s2) @@ -10,9 +11,9 @@ def checkInclusion(self, s1: str, s2: str) -> bool: return True l, r = 1, n1 while r < n2: - freq2[s2[l-1]] -= 1 - if freq2[s2[l-1]] == 0: - del freq2[s2[l-1]] + freq2[s2[l - 1]] -= 1 + if freq2[s2[l - 1]] == 0: + del freq2[s2[l - 1]] freq2[s2[r]] += 1 if freq1 == freq2: return True @@ -20,10 +21,11 @@ def checkInclusion(self, s1: str, s2: str) -> bool: l += 1 return False + if __name__ == "__main__": - s1 = "ab" #update s1 - s2 = "eidbaooo" #update s2 - + s1 = "ab" # update s1 + s2 = "eidbaooo" # update s2 + sol = Solution() result = sol.checkInclusion(s1, s2) - print(result) \ No newline at end of file + print(result) From bd3bab2970362043042c61cc44381e868df1f907 Mon Sep 17 00:00:00 2001 From: manas-1110 <125336538+manas-1110@users.noreply.github.com> Date: Sat, 5 Oct 2024 14:25:12 +0530 Subject: [PATCH 3/4] Added a new code --- strings/permutation_in_string.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 strings/permutation_in_string.py diff --git a/strings/permutation_in_string.py b/strings/permutation_in_string.py new file mode 100644 index 000000000000..47db74a4d449 --- /dev/null +++ b/strings/permutation_in_string.py @@ -0,0 +1,30 @@ +from collections import Counter + +class Solution: + def check_inclusion(self, s1: str, s2: str) -> bool: + n1, n2 = len(s1), len(s2) + if n2 < n1: + return False + freq1, freq2 = Counter(s1), Counter(s2[0:n1]) + if freq1 == freq2: + return True + left, right = 1, n1 + while right < n2: + freq2[s2[left-1]] -= 1 + if freq2[s2[left-1]] == 0: + del freq2[s2[left-1]] # Remove characters with zero frequency + freq2[s2[right]] += 1 + if freq1 == freq2: + return True + right += 1 + left += 1 + return False + +# Test the function +if __name__ == "__main__": + s1 = "ab" + s2 = "eidbaooo" + + sol = Solution() + result = sol.check_inclusion(s1, s2) + print(result) # Should return True or False based on the input strings From ea87c243c38884f254767da793c0a45ed1ed0bb1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Oct 2024 08:56:02 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/permutation_in_string.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/strings/permutation_in_string.py b/strings/permutation_in_string.py index 47db74a4d449..4c270585bd8c 100644 --- a/strings/permutation_in_string.py +++ b/strings/permutation_in_string.py @@ -1,5 +1,6 @@ from collections import Counter + class Solution: def check_inclusion(self, s1: str, s2: str) -> bool: n1, n2 = len(s1), len(s2) @@ -10,9 +11,9 @@ def check_inclusion(self, s1: str, s2: str) -> bool: return True left, right = 1, n1 while right < n2: - freq2[s2[left-1]] -= 1 - if freq2[s2[left-1]] == 0: - del freq2[s2[left-1]] # Remove characters with zero frequency + freq2[s2[left - 1]] -= 1 + if freq2[s2[left - 1]] == 0: + del freq2[s2[left - 1]] # Remove characters with zero frequency freq2[s2[right]] += 1 if freq1 == freq2: return True @@ -20,11 +21,12 @@ def check_inclusion(self, s1: str, s2: str) -> bool: left += 1 return False + # Test the function if __name__ == "__main__": s1 = "ab" s2 = "eidbaooo" - + sol = Solution() result = sol.check_inclusion(s1, s2) print(result) # Should return True or False based on the input strings