-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumWindow.java
More file actions
59 lines (48 loc) · 1.48 KB
/
minimumWindow.java
File metadata and controls
59 lines (48 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
*/
class Solution {
public String minWindow(String s, String t) {
if(t.length()> s.length()) return "";
String res = "";
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(char c: t.toCharArray())
{
map.put(c,map.getOrDefault(c,0)+1);
}
int counter = map.size();
int begin = 0; int end = 0;int head =0;
int len = Integer.MAX_VALUE;
while(end < s.length()){
char c = s.charAt(end);
if( map.containsKey(c) ){
map.put(c, map.get(c)-1);
if(map.get(c) == 0) counter--;
}
end++;
while(counter == 0)
{
char tempc = s.charAt(begin);
if(map.containsKey(tempc))
{
map.put(tempc,map.get(tempc)+1);
if(map.get(tempc) > 0)
{
counter++;
}
}
if(end-begin < len)
{
len = end-begin;
head = begin;
res = s.substring(head,end);
}
begin++;
}
}
if(len == Integer.MAX_VALUE) return "";
return res;
}
}
//madbahudeno