-
Notifications
You must be signed in to change notification settings - Fork 2
/
MinimumWindowSubstring.java
127 lines (100 loc) · 2.74 KB
/
MinimumWindowSubstring.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.algorithm.playground.leetcode.problems.lc0.lc70.lc76;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* https://leetcode.com/problems/minimum-window-substring/description/
*/
@SuppressWarnings("Duplicates")
public class MinimumWindowSubstring {
class Solution {
public String minWindow(String s, String t) {
Map<Character, Integer> counts = new HashMap<>();
for (Character ch : t.toCharArray()) {
int val = counts.getOrDefault(ch, 0) + 1;
counts.put(ch, val);
}
int required = counts.size();
int formed = 0;
int[] ans = new int[]{Integer.MAX_VALUE, 0, 0};
int l = 0;
Map<Character, Integer> window = new HashMap<>();
for (int r = 0; r < s.length(); r++) {
char ch = s.charAt(r);
int val = window.getOrDefault(ch, 0) + 1;
window.put(ch, val);
if (counts.containsKey(ch) && val == counts.get(ch)) {
formed++;
}
while (l <= r && formed == required) {
if (r - l < ans[0]) {
ans[0] = r - l;
ans[1] = l;
ans[2] = r + 1;
}
ch = s.charAt(l);
val = window.get(ch) - 1;
window.put(ch, val);
if (counts.containsKey(ch) && val < counts.get(ch)) {
formed--;
}
l++;
}
}
if (ans[0] != Integer.MAX_VALUE) {
return s.substring(ans[1], ans[2]);
}
return "";
}
}
class SolutionOptimized {
public String minWindow(String s, String t) {
Map<Character, Integer> counts = new HashMap<>();
for (Character ch : t.toCharArray()) {
int val = counts.getOrDefault(ch, 0) + 1;
counts.put(ch, val);
}
int required = counts.size();
int formed = 0;
int[] ans = new int[]{Integer.MAX_VALUE, 0, 0};
int l = 0;
StringBuilder builder = new StringBuilder();
List<Integer> indexes = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (counts.containsKey(ch)) {
builder.append(ch);
indexes.add(i);
}
}
Map<Character, Integer> window = new HashMap<>();
for (int r = 0; r < builder.length(); r++) {
char ch = builder.charAt(r);
int val = window.getOrDefault(ch, 0) + 1;
window.put(ch, val);
if (counts.containsKey(ch) && val == counts.get(ch)) {
formed++;
}
while (l <= r && formed == required) {
if (indexes.get(r) - indexes.get(l) < ans[0]) {
ans[0] = indexes.get(r) - indexes.get(l);
ans[1] = indexes.get(l);
ans[2] = indexes.get(r) + 1;
}
ch = builder.charAt(l);
val = window.get(ch) - 1;
window.put(ch, val);
if (counts.containsKey(ch) && val < counts.get(ch)) {
formed--;
}
l++;
}
}
if (ans[0] != Integer.MAX_VALUE) {
return s.substring(ans[1], ans[2]);
}
return "";
}
}
}