We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 6136c66 + bd3bab2 commit 83e1d4aCopy full SHA for 83e1d4a
strings/permutation_in_string.py
@@ -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
18
19
+ right += 1
20
+ left += 1
21
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