题目描述
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" |
使用一个hashMap存放s1中的字符,用字符作为键,出现的次数作为值。
然后使用一个长度为s1.length()的窗口在s2上滑动,并且记录窗口中的字符串的所有字符组成的hashMap2。在滑动的同时对hashMap2进行动态的更改,然后每一次滑动都进行一次比较,如果hashMap与hashMap2中的键和值都相同,则返回True,如果到窗口滑动结束后都没有匹配,则返回False。
代码实现
1 | class Solution { |