mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
kt: add «76. Minimum Window Substring»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
f88f860d0d
commit
0ad2295875
1 changed files with 35 additions and 0 deletions
35
kt/minimum-window-substring.kt
Normal file
35
kt/minimum-window-substring.kt
Normal file
|
@ -0,0 +1,35 @@
|
|||
class Solution {
|
||||
fun minWindow(s: String, t: String): String {
|
||||
val freqs = IntArray(128) { 0 }
|
||||
t.forEach {
|
||||
freqs[it.toInt()]++
|
||||
}
|
||||
|
||||
var (bestLower, bestUpper) = 0 to -1
|
||||
var count = t.length
|
||||
var (l, r) = 0 to 0
|
||||
|
||||
while (r < s.length) {
|
||||
if (freqs[s[r].toInt()]-- > 0) {
|
||||
count--
|
||||
}
|
||||
|
||||
while (count == 0) {
|
||||
if (bestLower >= bestUpper || bestUpper - bestLower + 1 > r - l + 1) {
|
||||
bestLower = l
|
||||
bestUpper = r
|
||||
}
|
||||
|
||||
freqs[s[l].toInt()]++
|
||||
|
||||
if (freqs[s[l++].toInt()] > 0) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
r++
|
||||
}
|
||||
|
||||
return s.substring(bestLower, bestUpper + 1)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue