Skip to content

Permutation of a String #11769

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
31 changes: 31 additions & 0 deletions strings/Permutation_in_String.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from collections import Counter

Check failure on line 1 in strings/Permutation_in_String.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

strings/Permutation_in_String.py:1:1: N999 Invalid module name: 'Permutation_in_String'


class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:

Check failure on line 5 in strings/Permutation_in_String.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

strings/Permutation_in_String.py:5:9: N802 Function name `checkInclusion` should be lowercase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: checkInclusion

As there is no test file in this pull request nor any test function or class in the file strings/Permutation_in_String.py, please provide doctest for the function checkInclusion

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

Check failure on line 12 in strings/Permutation_in_String.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E741)

strings/Permutation_in_String.py:12:9: E741 Ambiguous variable name: `l`
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

Check failure on line 21 in strings/Permutation_in_String.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E741)

strings/Permutation_in_String.py:21:13: E741 Ambiguous variable name: `l`
return False


if __name__ == "__main__":
s1 = "ab" # update s1
s2 = "eidbaooo" # update s2

sol = Solution()
result = sol.checkInclusion(s1, s2)
print(result)
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:

Check failure on line 3 in strings/permutation_in_string.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

strings/permutation_in_string.py:1:1: I001 Import block is un-sorted or un-formatted
def check_inclusion(self, s1: str, s2: str) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file strings/permutation_in_string.py, please provide doctest for the function check_inclusion

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"

Check failure on line 27 in strings/permutation_in_string.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/permutation_in_string.py:27:1: W293 Blank line contains whitespace
sol = Solution()
result = sol.check_inclusion(s1, s2)
print(result) # Should return True or False based on the input strings
Loading