//leetcode submit region begin(Prohibit modification and deletion) classSolution{ public String minWindow(String s, String t){ int sLen = s.length(); int tLen = t.length();
if (sLen < tLen) { return""; }
Map<Character, Integer> tMap = new HashMap<>();
for (int i = 0; i < tLen; i++) { tMap.put(t.charAt(i), tMap.getOrDefault(t.charAt(i), 0) + 1); }
int left = 0; String res = ""; int resSize = Integer.MAX_VALUE;
Map<Character, Integer> wins = new HashMap<>(); for (int i = 0; i < sLen; i++) { // 放入窗口 if (tMap.containsKey(s.charAt(i))) { wins.put(s.charAt(i), wins.getOrDefault(s.charAt(i), 0) + 1); } // 如果包含了 left+1 直到不包含 while (tMap.size() == wins.size() && mapsContains(wins, tMap) && left <= i) { // 记录 if (i - left + 1 < resSize) { resSize = i - left + 1; res = s.substring(left, i + 1); } char leftValue = s.charAt(left); if (wins.containsKey(leftValue)) { // 删除 wins.put(leftValue, wins.get(leftValue) - 1); } left++; } } return res; }
public Boolean mapsContains(Map<Character, Integer> map1, Map<Character, Integer> map2){ if (map1.size() < map2.size()) { returnfalse; } for (Character c : map2.keySet()) { if (!map1.containsKey(c)) { returnfalse; } if (map1.get(c) < map2.get(c)) { returnfalse; } }
returntrue; } } //leetcode submit region end(Prohibit modification and deletion)