LeetCode 76: Minimum Window Substring
2017-07-23 08:21
495 查看
1 public class Solution { 2 public String minWindow(String s, String t) { 3 if (s.length() == 0 || s.length() < t.length() || t.length() == 0) { 4 return ""; 5 } 6 Map<Character, Integer> charMap = new HashMap<>(); 7 for (char c : t.toCharArray()) { 8 charMap.put(c, charMap.getOrDefault(c, 0) + 1); 9 } 10 int totalCount = 0; 11 int length = Integer.MAX_VALUE; 12 int startPoint = 0; 13 14 15 for (int left = 0, right = 0; right < s.length(); right++) { 16 if (charMap.containsKey(s.charAt(right))) { 17 charMap.put(s.charAt(right), charMap.get(s.charAt(right)) - 1); 18 if (charMap.get(s.charAt(right)) == 0) { 19 totalCount++; 20 } 21 } 22 23 while (totalCount == charMap.size()) { 24 if (length > right - left + 1) { 25 length = right - left + 1; 26 startPoint = left; 27 } 28 29 if (charMap.containsKey(s.charAt(left))) { 30 charMap.put(s.charAt(left), charMap.get(s.charAt(left)) + 1); 31 if (charMap.get(s.charAt(left)) > 0) { 32 totalCount--; 33 } 34 } 35 left++; 36 } 37 } 38 return length == Integer.MAX_VALUE ? "" : s.substring(startPoint, startPoint + length); 39 } 40 }
FOLLOW UPS:
What if the input is coming streaming and you cannot even store the window? But you can store the checked String. I purposed this algorithm that store the index of the second String since the chars that not in the second String are useless. You can skipp them.
1 public class Solution { 2 private int length; 3 4 public String minWindow(String s, String t) { 5 if (s.length() == 0 || s.length() < t.length() || t.length() == 0) { 6 return ""; 7 } 8 Map<Character, LinkedList<Integer>> charMap = new HashMap<>(); 9 Map<Character, Integer> countMap = new HashMap<>(); 10 11 for (char c : t.toCharArray()) { 12 countMap.put(c, countMap.getOrDefault(c, 0) + 1); 13 } 14 15 String result = ""; 16 length = Integer.MAX_VALUE; 17 int total = 0; 18 19 for (int i = 0; i < s.length(); i++) { 20 char current = s.charAt(i); 21 if (countMap.containsKey(current)) { 22 if (!charMap.containsKey(current)) { 23 charMap.put(current, new LinkedList<>()); 24 } 25 LinkedList q = charMap.get(current); 26 q.offer(i); 27 28 if (q.size() > countMap.get(current)) { 29 q.poll(); 30 } 31 32 charMap.put(current, q); 33 34 result = checkMax(charMap, countMap, result, s); 35 } 36 } 37 return result; 38 } 39 40 private String checkMax(Map<Character, LinkedList<Integer>> charMap, Map<Character, Integer> countMap, String result, 41 String s) { 42 int min = Integer.MAX_VALUE; 43 int max = -1; 44 for (char c : countMap.keySet()) { 45 if (!charMap.containsKey(c) || charMap.get(c).size() != countMap.get(c)) { 46 return ""; 47 } 48 49 min = Math.min(min, charMap.get(c).getFirst()); 50 max = Math.max(max, charMap.get(c).getLast()); 51 } 52 53 if (max - min + 1 < length) { 54 length = max - min + 1; 55 return s.substring(min, max + 1); 56 } 57 return length == Integer.MAX_VALUE ? "" : result; 58 } 59 }
相关文章推荐
- [leetcode 76] Minimum Window Substring
- leetcode[76] Minimum Window Substring
- Leetcode 76 Minimum Window Substring(最小窗口的子串)
- LeetCode-76-Minimum Window Substring 尺取法+字典
- LeetCode76 Minimum Window Substring(两种解法)
- LeetCode 76 Minimum Window Substring (尺取法 推荐)
- LeetCode 76 Minimum Window Substring(Python详解及实现)
- 【leetcode 76】 Minimum Window Substring
- 【LeetCode 76】 Minimum Window Substring
- LeetCode76 Minimum Window Substring(两种解法)
- leetcode hard模式专杀之76 Minimum Window Substring
- leetcode 76:Minimum Window Substring
- LeetCode 76 minimum window substring
- [leetcode-76]Minimum Window Substring(java)
- LeetCode76 Minimum Window Substring(两种解法)
- leetcode 76 Minimum Window Substring
- leetcode[76]Minimum Window Substring
- LeetCode76 Minimum Window Substring(两种解法)
- LeetCode76 Minimum Window Substring(两种解法)
- LeetCode_OJ【76】Minimum Window Substring