-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumWindowSubString.swift
More file actions
59 lines (48 loc) · 1.68 KB
/
minimumWindowSubString.swift
File metadata and controls
59 lines (48 loc) · 1.68 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
76. Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
* If there is no such window in S that covers all characters in T, return the empty string "".
* If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
https://leetcode.com/problems/minimum-window-substring/
*/
class Solution {
func minWindow(_ s: String, _ t: String) -> String {
var dict = [Character: Int]()
for c in t {
dict[c, default: 0] += 1
}
var start = 0
var matched = 0
var minLength = Int.max
var subStringStart = -1
let strings = Array(s)
for end in 0..<strings.count {
let c = strings[end]
if let count = dict[c] {
dict[c] = count - 1
if count - 1 == 0 {
matched += 1
}
}
while matched == dict.count {
if end - start + 1 < minLength {
minLength = end - start + 1
subStringStart = start
}
let c = strings[start]
if let count = dict[c] {
if count == 0 {
matched -= 1
}
dict[c] = count + 1
}
start += 1
}
}
return subStringStart == -1 ? "" : String(strings[subStringStart..<subStringStart+minLength])
}
}