76. Minimum Window Substring
https://leetcode.com/problems/minimum-window-substring/discuss/26808/Here-is-a-10-line-template-that-can-solve-most-'substring'-problems
1 class Solution { 2 public String minWindow(String s, String t) { 3 if(s.length() == 0) return ""; 4 int[] map = new int[128]; 5 int count = 0; 6 int min = Integer.MAX_VALUE; 7 for(char c : t.toCharArray()){ 8 map[c]++; 9 count++; 10 } 11 int begin = 0, end = 0; 12 int index = 0; 13 while(end < s.length()){ 14 if(map[s.charAt(end)]-- > 0){ 15 count--; 16 } 17 end++; 18 while(count == 0){ 19 if(map[s.charAt(begin)]++ == 0){ 20 count++; 21 } 22 begin++; 23 if((end - begin + 1) < min){ //小于的话才更新index 24 min = end - begin + 1; 25 index = begin-1; 26 } 27 } 28 } 29 return min == Integer.MAX_VALUE? "" : s.substring(index, index + min); 30 31 } 32 }