publicclassSolution{ public List<Integer> slidingWindowTemplateByHarryChaoyangHe(String s, String t){ //init a collection or int value to save the result according the question. List<Integer> result = new LinkedList<>(); if(t.length()> s.length()) return result; //create a hashmap to save the Characters of the target substring. //(K, V) = (Character, Frequence of the Characters) Map<Character, Integer> map = new HashMap<>(); for(char c : t.toCharArray()){ map.put(c, map.getOrDefault(c, 0) + 1); } //maintain a counter to check whether match the target string. int counter = map.size();//must be the map size, NOT the string size because the char may be duplicate. //Two Pointers: begin - left pointer of the window; end - right pointer of the window int begin = 0, end = 0; //the length of the substring which match the target string. int len = Integer.MAX_VALUE; //loop at the begining of the source string while(end < s.length()){ char c = s.charAt(end);//get a character if( map.containsKey(c) ){ map.put(c, map.get(c)-1);// plus or minus one if(map.get(c) == 0) counter--;//modify the counter according the requirement(different condition). } end++; //increase begin pointer to make it invalid/valid again while(counter == 0/* counter condition. different question may have different condition */){ char tempc = s.charAt(begin);//***be careful here: choose the char at begin pointer, NOT the end pointer if(map.containsKey(tempc)){ map.put(tempc, map.get(tempc) + 1);//plus or minus one if(map.get(tempc) > 0) counter++;//modify the counter according the requirement(different condition). } begin++; } /* save / update(min/max) the result if find a target*/ // result collections or result int value } return result; } }
Longest Substring Without Repeating Characters: 这道题是可以使用我们的模板,也就是使用map记录出现的次数,如果次数大于1,代表不合乎条件,那么start进行运动。 一个优化方式是我们直接存储上一次遇到这个character的位置,这样我们使用start更新的时候可以一步到位,不用进行loop
//优化的 classSolution{ publicintlengthOfLongestSubstring(String s){ if(s.length()==0){ return0; } Map<Character, Integer> map = new HashMap<>(); int start = 0; int end = 0; int ans = 0; while(end<=s.length()-1){ char c = s.charAt(end); if(map.containsKey(c)){//当条件不成立的时候,就是我们使用start的时候 start = Math.max(start, map.get(c)+1); } map.put(c, end); ans = Math.max(ans, end-start+1); end++; } return ans; } }
Longest Substring with At Most Two Distinct Characters+340. Longest Substring with At Most K Distinct Characters: 套用我们的模板即可,counter一开始为零,每当我们的end遇到在map中不存在的character的时候,我们的count++;如果counter>k,说明我们现在有超过k中character,所以我们移动start直到某一个character在map中size为零,counter–。
/* 992. Subarrays with K Different Integers Hard 521 11 Favorite Share Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K. (For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.) Return the number of good subarrays of A. Example 1: Input: A = [1,2,1,2,3], K = 2 Output: 7 Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]. Example 2: Input: A = [1,2,1,3,4], K = 3 Output: 3 Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4]. */
classSolution{ publicintsubarraysWithKDistinct(int[] A, int K){ return atMostK(A, K)-atMostK(A, K-1); } privateintatMostK(int[] A, int K){ int start = 0; int end = 0; Map<Integer, Integer> map = new HashMap<>(); int count = 0; int ans = 0; while(end<=A.length-1){ int num = A[end]; if(map.getOrDefault(num, 0)==0){ count++; } map.put(num, map.getOrDefault(num, 0)+1); end++; while(count>K){ int startNum = A[start]; map.put(startNum, map.get(startNum)-1); if(map.get(startNum)==0){ count--; } start++; } ans = ans + end - start;//the total number of subarrays ending at j that contain at most K distinct. } return ans; } }
/* 1248. Count Number of Nice Subarrays Medium 56 0 Favorite Share Given an array of integers nums and an integer k. A subarray is called nice if there are k odd numbers on it. Return the number of nice sub-arrays. Example 1: Input: nums = [1,1,2,1,1], k = 3 Output: 2 Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1]. Example 2: Input: nums = [2,4,6], k = 1 Output: 0 Explanation: There is no odd numbers in the array. Example 3: Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2 Output: 16 Constraints: 1 <= nums.length <= 50000 1 <= nums[i] <= 10^5 1 <= k <= nums.length */ classSolution{ publicintnumberOfSubarrays(int[] nums, int k){ return atMostK(nums, k) - atMostK(nums, k-1); } privateintatMostK(int[] nums, int k){ int start = 0; int end = 0; int count = 0; int ans = 0; while(end<=nums.length-1){ if(nums[end]%2==1){ count++; } end++; while(count>k){ if(nums[start]%2==1){ count--; } start++; } ans = ans + end -start; } return ans; } }