topic:
Chapter 567: Arrangement of Character Strings
Given two strings s1 and s2, write a function to determine whether s2 contains the permutation of s1.
In other words, one of the permutations of the first string is a substring of the second string.
Example 1:
Input: s1 = “ab” s2 = “eidbaooo”
Output: True
Explanation: s2 contains one of the permutations of s1 (“ba”).
Example 2:
Input: s1= “ab” s2 = “eidboaoo”
Output: False
note:
The input string contains only lowercase letters
. The length of both strings is between [1, 10,000]
Problem solution ideas:
1. Use the length of s1 as the window to perform sliding window operation on s2;
2. When the Counter obtained by the sliding window slice is equal to the Counter of s1, the problem condition is met;
3. If all windows fail, return False.
Problem solution python code:
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
from collections import Counter
len1 = len(s1)
len2 = len(s2)
c1 = Counter(s1)
for i in range(0, len2-len1+1):
if Counter(s2[i:i+len1]) == c1:
return True
return False
Author: a-qing-ge
Links: https://leetcode-cn.com/problems/permutation-in-string/solution/hua-chuang-hen-hao-li-jie-de-dai-ma-nai-qujgb /
Source: LeetCode https://leetcode-cn.com/problems/permutation-in-string/