Skip to content

Commit fddbef5

Browse files
committed
feat : minimum-window-substring
1 parent cf5f6c2 commit fddbef5

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

minimum-window-substring/ekgns33.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public String minWindow(String s, String t) {
3+
int[] freq = new int[128];
4+
char[] str = s.toCharArray();
5+
int minL = 0;
6+
int minLength = Integer.MAX_VALUE;
7+
int l = 0, r = 0, n = s.length();
8+
for(char c : t.toCharArray()) {
9+
freq[c-'A']++;
10+
}
11+
int resolved = t.length();
12+
while(r < n) {
13+
if(freq[str[r++] - 'A']-- > 0) {
14+
resolved--;
15+
}
16+
while(resolved == 0) {
17+
if(r - l < minLength) {
18+
minL = l;
19+
minLength = r - l;
20+
}
21+
if(freq[str[l] - 'A']++ == 0) {
22+
resolved++;
23+
}
24+
l++;
25+
}
26+
}
27+
if(minLength == Integer.MAX_VALUE) return "";
28+
return new String(str, minL, minLength);
29+
}
30+
}

0 commit comments

Comments
 (0)