1 Public classSolution {2 Publicstring Minwindow (string s, String t) {3 if(s.length () = = 0 | | s.length () < T.length () | | t.length () = = 0) {4 return"";5 }6Map<character, integer> charmap =NewHashmap<>();7 for(CharC:t.tochararray ()) {8Charmap.put (c, Charmap.getordefault (c, 0) + 1);9 }Ten intTotalCount = 0; One intLength =Integer.max_value; A intStartPoint = 0; - - the for(intleft = 0, right = 0; Right < S.length (); right++) { - if(Charmap.containskey (S.charat (right))) { -Charmap.put (S.charat (right), Charmap.get (S.charat)-1); - if(Charmap.get (S.charat (right)) = = 0) { +totalcount++; - } + } A at while(TotalCount = =charmap.size ()) { - if(Length > Right-left + 1) { -Length = Right-left + 1; -StartPoint =Left ; - } - in if(Charmap.containskey (S.charat (left))) { -Charmap.put (S.charat (left), Charmap.get (S.charat (left)) + 1); to if(Charmap.get (S.charat (left)) > 0) { +totalcount--; - } the } *left++; $ }Panax Notoginseng } - returnLength = = Integer.max_value? "": S.substring (StartPoint, StartPoint +length); the } +}
Leetcode 76:minimum Window Substring