Skip to content

Commit e77ba97

Browse files
committed
Add minimum window string solution
1 parent cdba760 commit e77ba97

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package java1.algorithms.strings;
2+
// Sliding window: TC: O(m) + O(n) SC: O(m) + O(n)
3+
4+
import java.util.HashMap;
5+
6+
public class MinWindowSubstring {
7+
private static String minWindowSubstring(String windowStr, String subStr) {
8+
HashMap<Character, Integer> subStrCount = new HashMap<>();
9+
HashMap<Character, Integer> windowStrCount = new HashMap<>();
10+
11+
int minLength = Integer.MAX_VALUE;
12+
int leftIndex = -1, rightIndex = -1;
13+
14+
for(int i=0; i< subStr.length(); i++) {
15+
char ch = subStr.charAt(i);
16+
subStrCount.put(ch, subStrCount.getOrDefault(ch, 0) + 1);
17+
}
18+
19+
int having = 0, required = subStrCount.size();
20+
int left = 0;
21+
for(int right = 0; right < windowStr.length(); right++) {
22+
char rightChar = windowStr.charAt(right);
23+
windowStrCount.put(rightChar, windowStrCount.getOrDefault(rightChar, 0) + 1);
24+
if(subStrCount.containsKey(rightChar) && subStrCount.get(rightChar).intValue() == windowStrCount.get(rightChar).intValue()) {
25+
having++;
26+
}
27+
28+
while(required == having) {
29+
if(minLength > right-left+1) {
30+
minLength = right-left+1;
31+
leftIndex = left;
32+
rightIndex = right;
33+
}
34+
char leftChar = windowStr.charAt(left);
35+
windowStrCount.put(leftChar, windowStrCount.get(leftChar)-1);
36+
if(subStrCount.containsKey(leftChar) && subStrCount.get(leftChar).intValue() > windowStrCount.get(leftChar).intValue()) {
37+
having--;
38+
}
39+
left++;
40+
}
41+
}
42+
if(leftIndex == -1 || rightIndex == -1) {
43+
return "";
44+
} else {
45+
return windowStr.substring(leftIndex, rightIndex+1);
46+
}
47+
48+
}
49+
50+
public static void main(String[] args) {
51+
String s ="ADOBECODEBANC", t= "ABC";
52+
System.out.println(minWindowSubstring(s, t));
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Sliding window: TC: O(m) + O(n) SC: O(m) + O(n)
2+
3+
function minWindowSubstring(windowStr, subStr) {
4+
let windowStrCount = new Map();
5+
let subStrCount = new Map();
6+
7+
let minLength = Number.MAX_VALUE;
8+
let leftIndex = rightIndex = -1;
9+
10+
for(const ch of subStr) {
11+
subStrCount.set(ch, (subStrCount.get(ch) || 0) + 1);
12+
}
13+
14+
let having = 0; required = subStrCount.size;
15+
let left = right = 0;
16+
17+
for(right = 0; right < windowStr.length; right++) {
18+
let rightChar = windowStr.charAt(right);
19+
windowStrCount.set(rightChar, (windowStrCount.get(right) || 0) +1);
20+
if(subStrCount.has(rightChar) && subStrCount.get(rightChar) === windowStrCount.get(rightChar)) {
21+
having++;
22+
}
23+
24+
while(required === having) {
25+
if(minLength > right-left+1) {
26+
minLength = right-left+1;
27+
leftIndex = left;
28+
rightIndex = right;
29+
}
30+
31+
let leftChar = windowStr.charAt(left);
32+
windowStrCount.set(leftChar, windowStrCount.get(leftChar)-1);
33+
if(subStrCount.has(leftChar) && subStrCount.get(leftChar) > windowStrCount.get(leftChar)) {
34+
having--;
35+
}
36+
left++;
37+
}
38+
}
39+
if(leftIndex === -1 || rightIndex === -1) {
40+
return "";
41+
} else {
42+
return windowStr.substring(leftIndex, rightIndex+1);
43+
}
44+
45+
}
46+
47+
let s ="ADOBECODEBANC";
48+
let t= "ABC";
49+
console.log(minWindowSubstring(s, t));

0 commit comments

Comments
 (0)