1 滑动窗口大小不变
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/597caf9b057c252c806c00c0c27387d4.png)
public boolean checkInclusion(String s1, String s2) {
char[] str1 = s1.toCharArray();
int m = s1.length();
char[] str2 = s2.toCharArray();
if(s1.length() > s2.length()){
return false;
}
int[] arr1 = new int[26];
int[] arr2 = new int[26];
for (int i = 0; i < str1.length; i++) {
arr1[str1[i] - 'a']++;
arr2[str2[i] - 'a']++;
}
if(judge(arr1, arr2)){
return true;
}
for (int i = m; i < s2.length(); i++) {
arr2[str2[i - m] - 'a']--;
arr2[str2[i] - 'a']++;
if(judge(arr1, arr2)){
return true;
}
}
return false;
}
private boolean judge(int[] arr1, int[] arr2) {
for (int i = 0; i < 26; i++) {
if(arr1[i] != arr2[i]){
return false;
}
}
return true;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/75e322ec7e714c80389effcc4f0309ec.png)
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<>();
char[] str1 = s.toCharArray();
char[] str2 = p.toCharArray();
if(str1.length < str2.length){
return res;
}
int m = p.length();
int[] arr1 = new int[26];
int[] arr2 = new int[26];
for (int i = 0; i < str2.length; i++) {
arr2[str2[i] - 'a']++;
arr1[str1[i] - 'a']++;
}
if(judge(arr1, arr2)){
res.add(0);
}
for (int i = m; i < str1.length; i++) {
arr1[str1[i - m] - 'a']--;
arr1[str1[i] - 'a']++;
if(judge(arr1, arr2)){
res.add(i - m + 1);
}
}
return res;
}
private boolean judge(int[] arr1, int[] arr2) {
for (int i = 0; i < 26; i++) {
if(arr1[i] != arr2[i]){
return false;
}
}
return true;
}
2 滑动窗口大小会改变
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4c8d217638c17835e1ff7357cce1aae0.png)
public String minWindow(String s, String t) {
char[] str1 = s.toCharArray();
char[] str2 = t.toCharArray();
int N1 = s.length();
int N2 = t.length();
int[] res = new int[2];
Map<Character, Integer> map = new HashMap<>();
int left = 0;
int right = 0;
int count = str2.length;
int windowLen = Integer.MAX_VALUE;
for (int i = 0; i < str2.length; i++) {
map.put(str2[i], map.getOrDefault(str2[i], 0) + 1);
}
while(right < N1){
if(map.containsKey(str1[right])){
if(map.get(str1[right]) > 0){
count--;
}
map.put(str1[right], map.get(str1[right]) - 1);
}
while(count == 0){
if(windowLen > right - left){
windowLen = right - left;
res[0] = left;
res[1] = right;
}
if(map.containsKey(str1[left])){
map.put(str1[left], map.get(str1[left]) + 1);
if(map.get(str1[left]) > 0){
count++;
}
}
left++;
}
right++;
}
if(windowLen == Integer.MAX_VALUE){
return "";
}
return s.substring(res[0], res[1] + 1);
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bfab217c23c7df4dc39e46c07120291f.png)
public int[] shortestSeq(int[] big, int[] small) {
int left = 0;
int right = 0;
int N = big.length;
int[] res = {};
int count = small.length;
int minLen = N;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < small.length; i++) {
map.put(small[i], 1);
}
while (right < N) {
if (map.containsKey(big[right])) {
if (map.get(big[right]) > 0) {
count--;
}
map.put(big[right], map.get(big[right]) - 1);
}
while (count == 0) {
if(minLen > right - left){
minLen = right - left;
res = new int[]{left, right};
}
if(map.containsKey(big[left])){
map.put(big[left], map.get(big[left]) + 1);
if(map.get(big[left]) > 0){
count++;
}
}
left++;
}
right++;
}
return res;
}