-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminWindow.java
More file actions
74 lines (63 loc) · 2.51 KB
/
Copy pathminWindow.java
File metadata and controls
74 lines (63 loc) · 2.51 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
Note:
If there is no such window in S that covers all characters in T, return the empty string "".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
*/
public class Solution {
public String minWindow(String s, String t) {
HashMap<Character, Integer> target = new HashMap<Character, Integer>(); // Target string
HashMap<Character, Integer> found = new HashMap<Character, Integer>(); // Found character map
int i = 0;
char c,ch;
for(i = 0; i< t.length(); i++){
c = t.charAt(i);
if(target.containsKey(c)){
target.put(c, target.get(c) + 1); // if already exists we remember the frequency because there could be duplicate characters
}
else {
target.put(c,1);
}
}
int start = 0;
int end = 0;
int minLen = Integer.MAX_VALUE;
int count = 0;
String result = "";
while( end < s.length() ){
c = s.charAt(end);
if(target.containsKey(c)){
if(found.containsKey(c)){ // means there are duplicate characters in target
if(found.get(c) < target.get(c)){
count++; // character count of target
}
found.put(c, found.get(c)+1); // remembering frequency of the duplicate character
}
else {
count++;
found.put(c,1);
}
}
if( count == t.length() ) {
ch = s.charAt(start);
while(!found.containsKey(ch) || found.get(ch) > target.get(ch)){ // either character got removed while removing duplicate or new window started
if(found.containsKey(ch) && found.get(ch) > target.get(ch)){
found.put(ch, found.get(ch) - 1);
}
start++;
ch = s.charAt(start);
}
if(end - start + 1 < minLen){
result = s.substring(start,end+1);
minLen = end - start + 1;
}
}
end++;
}
return result;
}
}