MinimumWindowSubstring { public String minWindow(String s, String t) { HashMap<Character, Integer> tMap = new HashMap<>(); for (char c: t.toCharArray()) { tMap.put(c, tMap.getOrDefault(c, 0) + 1); } int max = s.length() + 1, start = -1; HashMap<Character, Integer> windowMap = new HashMap<>(); Queue<Integer> queue = new LinkedList<>(); for (int i =0; i < s.length(); i++) { char c = s.charAt(i); if (tMap.containsKey(c)) { queue.offer(i); windowMap.put(c, windowMap.getOrDefault(c, 0) + 1); if (lessThan(tMap, windowMap)) { while (!queue.isEmpty() && lessThan(tMap, windowMap)) { int p = queue.poll(); int len = i - p + 1; if (len < max) { max = len; start = p; } char ctop = s.charAt(p); if (queue.size() > 0) { ctop = s.charAt(p); windowMap.put(ctop, windowMap.get(ctop) - 1); } } } } } return start == -1 ? "" : s.substring(start, start + max); }  String minWindow(String s, String t);  }
@Test(dataProvider = "testStrings") public void test(String s, String t, String expected) { assertEquals(new MinimumWindowSubstring().minWindow(s, t), expected); }