forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyhkee0404.kt
More file actions
33 lines (33 loc) · 1.04 KB
/
yhkee0404.kt
File metadata and controls
33 lines (33 loc) · 1.04 KB
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
class Solution {
fun minWindow(s: String, t: String): String { // T(m, n) = S(m, n) = O(m + n)
val counterT = (
t
.groupingBy {it} // O(n)
.eachCount()
)
var l = -1
var r = -1
val counterS = mutableMapOf<Char, Int>()
var i = -1
(
s
.withIndex()
.forEach { // O(m)
counterS.compute(it.value) {k, v -> if (v == null) 1 else v + 1}
while (i != it.index && counterS[s[i + 1]]!! > counterT.getOrDefault(s[i + 1], 0)) { // O(m)
counterS.compute(s[++i]) {k, v -> v!! - 1}
}
if (
counterT
.all {(k, v) -> counterS.getOrDefault(k, 0) >= v} // O(n)
) {
if (r == -1 || r - l > it.index - i) {
l = i
r = it.index
}
}
}
)
return s.substring(l + 1, r + 1) // O(m)
}
}