Skip to content

Added a new code #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions strings/permutation_in_string.py
Original file line number Diff line number Diff line change
@@ -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