Skip to content

Commit a118c1d

Browse files
week14 mission minimum-window-substring
1 parent d1b731f commit a118c1d

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
- 문제: https://leetcode.com/problems/minimum-window-substring/
2+
- 풀이: https://algorithm.jonghoonpark.com/2024/07/29/leetcode-76
3+
4+
## 버전 1
5+
6+
```java
7+
class Solution {
8+
public String minWindow(String s, String t) {
9+
if (s.length() < t.length()) {
10+
return "";
11+
}
12+
13+
Map<Character, Integer> counter = new HashMap<>();
14+
15+
for (int i = 0; i < t.length(); i++) {
16+
counter.merge(t.charAt(i), 1, Integer::sum);
17+
}
18+
19+
int start = 0;
20+
21+
int minLength = 0;
22+
int minStart = 0;
23+
24+
for (int i = 0; i < s.length(); i++) {
25+
// update counter
26+
char currentChar = s.charAt(i);
27+
28+
Integer count = counter.get(currentChar);
29+
if (count != null) {
30+
counter.merge(currentChar, -1, Integer::sum);
31+
} else {
32+
continue;
33+
}
34+
35+
// check all count is 0 -> update min;
36+
while (validate(counter)) {
37+
int currentLength = i + 1 - start;
38+
if (minLength > currentLength || minLength == 0) {
39+
minStart = start;
40+
minLength = currentLength;
41+
}
42+
43+
// try to move start to start + 1
44+
// increase count if front char exist in counter
45+
count = counter.get(s.charAt(start));
46+
if (count != null) {
47+
if (count == 0) {
48+
break;
49+
}
50+
51+
counter.put(s.charAt(start), count + 1);
52+
}
53+
start++;
54+
}
55+
}
56+
57+
return s.substring(minStart, minStart + minLength);
58+
}
59+
60+
private boolean validate(Map<Character, Integer> counter) {
61+
for (int count : counter.values()) {
62+
if (count > 0) {
63+
return false;
64+
}
65+
}
66+
return true;
67+
}
68+
}
69+
```
70+
71+
### TC, SC
72+
73+
λ¬Έμ œμ— λ‹€μŒκ³Ό 같이 μ œμ‹œλ˜μ–΄ μžˆλ‹€.
74+
75+
```java
76+
m == s.length
77+
n == t.length
78+
```
79+
80+
μ‹œκ°„ λ³΅μž‘λ„λŠ” `O(n * m)`, 곡간 λ³΅μž‘λ„λŠ” `O(n)` 이닀.
81+
82+
## 버전 2: μ„±λŠ₯ μš°μ„  버전
83+
84+
사싀 이 버전은 μ‹€μ œ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œλŠ” μ“°μ§€ μ•Šμ„μ§€λ„ λͺ¨λ₯΄κ² λ‹€. μ΄ν•΄ν•˜λŠ”λ° μ’€ 더 μ‹œκ°„μ΄ 걸릴 것 κ°™κΈ° λ•Œλ¬Έμ΄λ‹€.
85+
86+
```java
87+
class Solution {
88+
public String minWindow(String s, String t) {
89+
if (s.length() < t.length()) {
90+
return "";
91+
}
92+
93+
Integer[] counter = new Integer[123]; // μ λ‹Ήνžˆ λ„‰λ„‰ν•œ λ°°μ—΄ ν• λ‹Ή
94+
95+
char[] mustContainCharArray = t.toCharArray();
96+
for (char mustContainChar : mustContainCharArray) {
97+
int count = counter[mustContainChar] == null ? 0 : counter[mustContainChar];
98+
count = count + 1;
99+
counter[mustContainChar] = count;
100+
}
101+
102+
int start = 0;
103+
104+
int minLength = 0;
105+
int minLengthFrom = 0;
106+
107+
for (int i = 0; i < s.length(); i++) {
108+
char currentChar = s.charAt(i);
109+
110+
Integer count = counter[currentChar];
111+
if (count != null) {
112+
count = count - 1;
113+
counter[currentChar] = count;
114+
}
115+
116+
// check all count is 0 -> update min;
117+
while (validate(counter)) {
118+
int currentLength = i + 1 - start;
119+
if (minLength > currentLength || minLength == 0) {
120+
minLengthFrom = start;
121+
minLength = currentLength;
122+
}
123+
124+
// try to move start to start + 1
125+
// increase count if front char exist in counter
126+
count = counter[s.charAt(start)];
127+
if (count != null) {
128+
if (count == 0) {
129+
break;
130+
}
131+
132+
counter[s.charAt(start)] = count + 1;
133+
}
134+
start++;
135+
}
136+
}
137+
138+
return s.substring(minLengthFrom, minLengthFrom + minLength);
139+
}
140+
141+
private boolean validate(Integer[] counter) {
142+
for (int i = 65; i < counter.length; i++) { // 65λΆ€ν„° μ•ŒνŒŒλ²³μ΄ μ‹œμž‘ν•˜κΈ° λ•Œλ¬Έμ— κ·Έ 이전 값은 μ˜λ―Έμ—†λ‹€.
143+
Integer count = counter[i];
144+
if (count == null) {
145+
continue;
146+
}
147+
148+
if (count > 0) {
149+
return false;
150+
}
151+
}
152+
return true;
153+
}
154+
}
155+
```
156+
157+
### TC, SC
158+
159+
λ¬Έμ œμ— λ‹€μŒκ³Ό 같이 μ œμ‹œλ˜μ–΄ μžˆλ‹€.
160+
161+
```java
162+
m == s.length
163+
n == t.length
164+
```
165+
166+
μ‹œκ°„ λ³΅μž‘λ„λŠ” `O(n*m)`, 곡간 λ³΅μž‘λ„λŠ” `O(1)` 이닀. ν‘œν˜„μƒμœΌλ‘œλŠ” μ‹œκ°„λ³΅μž‘λ„λŠ” λ™μΌν•˜λ‚˜ μ‹€μ œ μ‹€ν–‰ μ‹œκ°„μ€ μ€„μ–΄λ“€μ—ˆκ³ , κ³΅κ°„λ³΅μž‘λ„λŠ” μ€„μ–΄λ“€κ²Œ λ˜μ—ˆλ‹€.

0 commit comments

Comments
Β (0)