LeetCode 567. Permutation in String
题目描述:
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string.
Example 1:
1 | Input:s1 = "ab" s2 = "eidbaooo" |
Example 2:
1 | Input:s1= "ab" s2 = "eidboaoo" |
Note:
- The input strings only contain lower case letters.
- The length of both given strings is in range [1, 10,000].
双指针,在s2中寻找一个连续子串,其中包含所有的s1中的字符并且没有其他字符。用一个哈希表记录s1中每个字符的出现次数,然后右指针前进,直到左右指针之间的子串不满足条件(也就是有字符的出现次数多于其在s1中的出现次数),再向前移动左指针直到满足条件,当子串长度等于s1的长度时返回true,如果没有这样的子串返回false。
1 | class Solution { |