Skip to content

Commit bda8809

Browse files
authored
feat: add solutions to lc problem: No.0076 (doocs#758)
No.0076.Minimum Window Substring
1 parent 01f3f4f commit bda8809

File tree

4 files changed

+261
-26
lines changed

4 files changed

+261
-26
lines changed

solution/0000-0099/0076.Minimum Window Substring/README.md

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,49 @@
7474
<!-- 这里可写当前语言的特殊实现逻辑 -->
7575

7676
```java
77+
class Solution {
78+
public String minWindow(String s, String t) {
79+
Map<Character, Integer> mp = new HashMap<>();
80+
int begin = 0, end = 0, counter = t.length(), minLen = Integer.MAX_VALUE, minStart = 0, size = s.length();
81+
82+
for (char c : s.toCharArray())
83+
mp.put(c, 0);
84+
for (char c : t.toCharArray()) {
85+
if (mp.containsKey(c))
86+
mp.put(c, mp.get(c) + 1);
87+
else
88+
return "";
89+
}
90+
91+
while (end < size) {
92+
if (mp.get(s.charAt(end)) > 0)
93+
counter--;
94+
95+
mp.put(s.charAt(end), mp.get(s.charAt(end)) - 1);
96+
97+
end++;
98+
99+
while (counter == 0) {
100+
if (end - begin < minLen) {
101+
minStart = begin;
102+
minLen = end - begin;
103+
}
104+
mp.put(s.charAt(begin), mp.get(s.charAt(begin)) + 1);
105+
106+
if (mp.get(s.charAt(begin)) > 0) {
107+
counter++;
108+
}
77109

110+
begin++;
111+
}
112+
}
113+
114+
if (minLen != Integer.MAX_VALUE) {
115+
return s.substring(minStart, minStart + minLen);
116+
}
117+
return "";
118+
}
119+
}
78120
```
79121

80122
### **TypeScript**
@@ -119,10 +161,45 @@ function minWindow(s: string, t: string): string {
119161
}
120162
```
121163

122-
### **...**
164+
### **C++**
123165

124-
```
166+
```cpp
167+
class Solution {
168+
public:
169+
string minWindow(string s, string t){
170+
unordered_map<char, int> m;
171+
int begin = 0, end = 0, minlen = INT_MAX, minStart = 0, size = s.size(), counter = t.size();
172+
for (auto c: t)
173+
m[c]++;
174+
175+
while (end < size) {
176+
if (m[s[end]] > 0)
177+
counter--;
178+
179+
m[s[end]]--;
180+
181+
end++;
182+
183+
while (counter == 0) {
184+
if (end - begin < minlen) {
185+
minStart = begin;
186+
minlen = end - begin;
187+
}
188+
189+
m[s[begin]]++;
190+
if (m[s[begin]] > 0)
191+
counter++;
125192

193+
begin++;
194+
}
195+
}
196+
197+
if (minlen != INT_MAX) {
198+
return s.substr(minStart, minlen);
199+
}
200+
return "";
201+
}
202+
};
126203
```
127204

128205
<!-- tabs:end -->

solution/0000-0099/0076.Minimum Window Substring/README_EN.md

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,49 @@ Since the largest window of s only has one &#39;a&#39;, return empty string.
6262
### **Java**
6363

6464
```java
65+
class Solution {
66+
public String minWindow(String s, String t) {
67+
Map<Character, Integer> mp = new HashMap<>();
68+
int begin = 0, end = 0, counter = t.length(), minLen = Integer.MAX_VALUE, minStart = 0, size = s.length();
69+
70+
for (char c : s.toCharArray())
71+
mp.put(c, 0);
72+
for (char c : t.toCharArray()) {
73+
if (mp.containsKey(c))
74+
mp.put(c, mp.get(c) + 1);
75+
else
76+
return "";
77+
}
78+
79+
while (end < size) {
80+
if (mp.get(s.charAt(end)) > 0)
81+
counter--;
82+
83+
mp.put(s.charAt(end), mp.get(s.charAt(end)) - 1);
84+
85+
end++;
86+
87+
while (counter == 0) {
88+
if (end - begin < minLen) {
89+
minStart = begin;
90+
minLen = end - begin;
91+
}
92+
mp.put(s.charAt(begin), mp.get(s.charAt(begin)) + 1);
93+
94+
if (mp.get(s.charAt(begin)) > 0) {
95+
counter++;
96+
}
6597

98+
begin++;
99+
}
100+
}
101+
102+
if (minLen != Integer.MAX_VALUE) {
103+
return s.substring(minStart, minStart + minLen);
104+
}
105+
return "";
106+
}
107+
}
66108
```
67109

68110
### **TypeScript**
@@ -107,10 +149,45 @@ function minWindow(s: string, t: string): string {
107149
}
108150
```
109151

110-
### **...**
152+
### **C++**
111153

112-
```
154+
```cpp
155+
class Solution {
156+
public:
157+
string minWindow(string s, string t){
158+
unordered_map<char, int> m;
159+
int begin = 0, end = 0, minlen = INT_MAX, minStart = 0, size = s.size(), counter = t.size();
160+
for (auto c: t)
161+
m[c]++;
162+
163+
while (end < size) {
164+
if (m[s[end]] > 0)
165+
counter--;
166+
167+
m[s[end]]--;
168+
169+
end++;
170+
171+
while (counter == 0) {
172+
if (end - begin < minlen) {
173+
minStart = begin;
174+
minlen = end - begin;
175+
}
176+
177+
m[s[begin]]++;
178+
if (m[s[begin]] > 0)
179+
counter++;
113180

181+
begin++;
182+
}
183+
}
184+
185+
if (minlen != INT_MAX) {
186+
return s.substr(minStart, minlen);
187+
}
188+
return "";
189+
}
190+
};
114191
```
115192

116193
<!-- tabs:end -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
string minWindow(string s, string t){
4+
unordered_map<char, int> m;
5+
int begin = 0, end = 0, minlen = INT_MAX, minStart = 0, size = s.size(), counter = t.size();
6+
for (auto c: t)
7+
m[c]++;
8+
9+
while (end < size) {
10+
if (m[s[end]] > 0)
11+
counter--;
12+
13+
m[s[end]]--;
14+
15+
end++;
16+
17+
while (counter == 0) {
18+
if (end - begin < minlen) {
19+
minStart = begin;
20+
minlen = end - begin;
21+
}
22+
23+
m[s[begin]]++;
24+
if (m[s[begin]] > 0)
25+
counter++;
26+
27+
begin++;
28+
}
29+
}
30+
31+
if (minlen != INT_MAX) {
32+
return s.substr(minStart, minlen);
33+
}
34+
return "";
35+
}
36+
};
Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,73 @@
11
class Solution {
22
public String minWindow(String s, String t) {
3-
int[] count = new int['z' - 'A' + 1];
4-
int uniq = 0;
5-
for (int i = 0; i < t.length(); ++i) {
6-
if (++count[t.charAt(i) - 'A'] == 1) uniq++;
3+
Map<Character, Integer> mp = new HashMap<>();
4+
int begin = 0, end = 0, counter = t.length(), minLen = Integer.MAX_VALUE, minStart = 0, size = s.length();
5+
6+
for (char c : s.toCharArray())
7+
mp.put(c, 0);
8+
for (char c : t.toCharArray()) {
9+
if (mp.containsKey(c))
10+
mp.put(c, mp.get(c) + 1);
11+
else
12+
return "";
713
}
8-
int found = 0,i = 0,j = 0;
9-
int minLen = Integer.MAX_VALUE;
10-
int minJ = Integer.MAX_VALUE;
11-
while (found < uniq) {
12-
while (i < s.length()) {
13-
if (found >= uniq) break;
14-
if (--count[s.charAt(i) - 'A'] == 0) found++;
15-
i++;
14+
15+
while (end < size) {
16+
if (mp.get(s.charAt(end)) > 0)
17+
counter--;
18+
19+
mp.put(s.charAt(end), mp.get(s.charAt(end)) - 1);
20+
21+
end++;
22+
23+
while (counter == 0) {
24+
if (end - begin < minLen) {
25+
minStart = begin;
26+
minLen = end - begin;
27+
}
28+
mp.put(s.charAt(begin), mp.get(s.charAt(begin)) + 1);
29+
30+
if (mp.get(s.charAt(begin)) > 0) {
31+
counter++;
32+
}
33+
34+
begin++;
1635
}
17-
if (found < uniq) break;
18-
while (j < i && count[s.charAt(j) - 'A'] < 0) count[s.charAt(j++) - 'A']++;
19-
if (i - j < minLen) {
20-
minLen = i - j;
21-
minJ = j;
22-
}
23-
count[s.charAt(j++) - 'A']++;
24-
found--;
2536
}
26-
return minLen < Integer.MAX_VALUE ? s.substring(minJ, minJ + minLen) : "";
37+
38+
if (minLen != Integer.MAX_VALUE) {
39+
return s.substring(minStart, minStart + minLen);
40+
}
41+
return "";
2742
}
28-
}
43+
}
44+
45+
46+
// class Solution {
47+
// public String minWindow(String s, String t) {
48+
// int[] count = new int['z' - 'A' + 1];
49+
// int uniq = 0;
50+
// for (int i = 0; i < t.length(); ++i) {
51+
// if (++count[t.charAt(i) - 'A'] == 1) uniq++;
52+
// }
53+
// int found = 0,i = 0,j = 0;
54+
// int minLen = Integer.MAX_VALUE;
55+
// int minJ = Integer.MAX_VALUE;
56+
// while (found < uniq) {
57+
// while (i < s.length()) {
58+
// if (found >= uniq) break;
59+
// if (--count[s.charAt(i) - 'A'] == 0) found++;
60+
// i++;
61+
// }
62+
// if (found < uniq) break;
63+
// while (j < i && count[s.charAt(j) - 'A'] < 0) count[s.charAt(j++) - 'A']++;
64+
// if (i - j < minLen) {
65+
// minLen = i - j;
66+
// minJ = j;
67+
// }
68+
// count[s.charAt(j++) - 'A']++;
69+
// found--;
70+
// }
71+
// return minLen < Integer.MAX_VALUE ? s.substring(minJ, minJ + minLen) : "";
72+
// }
73+
// }

0 commit comments

Comments
 (0)