Skip to content

Commit 83e1d4a

Browse files
authored
Merge pull request #1 from manas-1110/neww
Added a new code
2 parents 6136c66 + bd3bab2 commit 83e1d4a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

strings/permutation_in_string.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
def check_inclusion(self, s1: str, s2: str) -> bool:
5+
n1, n2 = len(s1), len(s2)
6+
if n2 < n1:
7+
return False
8+
freq1, freq2 = Counter(s1), Counter(s2[0:n1])
9+
if freq1 == freq2:
10+
return True
11+
left, right = 1, n1
12+
while right < n2:
13+
freq2[s2[left-1]] -= 1
14+
if freq2[s2[left-1]] == 0:
15+
del freq2[s2[left-1]] # Remove characters with zero frequency
16+
freq2[s2[right]] += 1
17+
if freq1 == freq2:
18+
return True
19+
right += 1
20+
left += 1
21+
return False
22+
23+
# Test the function
24+
if __name__ == "__main__":
25+
s1 = "ab"
26+
s2 = "eidbaooo"
27+
28+
sol = Solution()
29+
result = sol.check_inclusion(s1, s2)
30+
print(result) # Should return True or False based on the input strings

0 commit comments

Comments
 (0)