567. Permutation in String

sliding window

 

 

 1 class Solution {
 2     public boolean checkInclusion(String s1, String s2) {
 3         if(s1.length() > s2.length()) return false;
 4         int[] arr = new int[128];
 5         int count = 0;
 6         for(int i = 0; i < s1.length(); i++){
 7             arr[s1.charAt(i)]++;
 8             count++;
 9         }
10         int i = 0, j = 0;
11         while(j < s2.length()){
12             if(arr[s2.charAt(j++)]-- > 0){
13                 count--;
14             }
15             if(j - i == s1.length()){
16                 if(count == 0){
17                     return true;
18                 }
19                 if(arr[s2.charAt(i++)]++ >= 0){
20                     count++;
21                 }
22             }
23         }
24         return false;
25         
26     }
27 }

 

posted @ 2018-10-29 09:58  jasoncool1  阅读(119)  评论(0编辑  收藏  举报