-
Notifications
You must be signed in to change notification settings - Fork 31
/
WindowString.java
81 lines (62 loc) · 2.03 KB
/
WindowString.java
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
75
76
77
78
79
80
81
package Hashing;
import java.util.HashMap;
import java.util.Map;
/**
* Author - archit.s
* Date - 30/10/18
* Time - 1:36 PM
*/
public class WindowString {
public String minWindow(String A, String B) {
Map<Character, Integer> reqCountMap = new HashMap<>();
Map<Character, Integer> windowCountMap = new HashMap<>();
int tempStart = 0, tempEnd = 0 , minS = -1, minE = Integer.MAX_VALUE-100;
for(int i=0;i<B.length();i++){
Character c = B.charAt(i);
reqCountMap.put(c, reqCountMap.getOrDefault(c,0)+1);
}
int reqL = B.length();
int doneL = 0;
while(true){
if(doneL<reqL){
if (tempEnd == A.length()){
break;
}
Character c = A.charAt(tempEnd);
if(reqCountMap.containsKey(c)){
windowCountMap.put(c, windowCountMap.getOrDefault(c,0)+1);
if(reqCountMap.get(c) >= windowCountMap.get(c)){
doneL++;
}
}
tempEnd++;
}
else if(doneL == reqL){
if((tempEnd-tempStart) < (minE - minS)){
minS = tempStart;
minE = tempEnd;
}
Character c = A.charAt(tempStart);
int count = windowCountMap.getOrDefault(c,0);
if(count>0){
count--;
windowCountMap.put(c, count);
if(count < reqCountMap.getOrDefault(c,0)){
doneL--;
}
}
tempStart++;
}
}
StringBuilder s = new StringBuilder();
if(minS >= 0 ){
for(int i=minS;i<minE;i++){
s.append(A.charAt(i));
}
}
return s.toString();
}
public static void main(String[] args) {
System.out.println(new WindowString().minWindow("ADOBECODEBANC", "ABC"));
}
}