-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0076.Minimum Window Substring.swift
51 lines (38 loc) · 1.44 KB
/
0076.Minimum Window Substring.swift
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
class Solution {
func minWindow(_ s: String, _ t: String) -> String {
guard s.count > 0, t.count > 0 else {
return ""
}
var charsS = Array(s)
var mapT = [Character:Int]()
for ch in t {
mapT[ch, default: 0] += 1
}
var found = 0
var left = 0, right = 0
var result: (count: Int, left: Int, right: Int) = (-1, left, right)
var mapWindow = [Character:Int]()
while right < charsS.count {
let ch = charsS[right]
mapWindow[ch, default: 0] += 1
if let chCount = mapT[ch], mapWindow[ch] == chCount {
found += 1
}
while left <= right, found == mapT.count {
let ch = charsS[left]
if result.count == -1 || right - left + 1 < result.count {
result.count = right - left + 1
result.left = left
result.right = right
}
mapWindow[ch]! -= 1
if mapT[ch] != nil && mapWindow[ch]! < mapT[ch]! {
found -= 1
}
left += 1
}
right += 1
}
return result.count == -1 ? "" : String(charsS[result.left...result.right])
}
}