-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMinimumWindowSubstring.swift
More file actions
48 lines (42 loc) Β· 1.34 KB
/
Copy pathMinimumWindowSubstring.swift
File metadata and controls
48 lines (42 loc) Β· 1.34 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
//
// MinimumWindowSubstring.swift
// Algorithm Solutions In Swift
//
// Created by Boudhayan Biswas on 04/02/24.
//
import Foundation
func minWindow(_ s: String, _ t: String) -> String {
guard s.count >= t.count else { return "" }
var sChars = Array(s)
let freq = Dictionary(Array(t).map { ($0, 1)}, uniquingKeysWith: +)
var l = 0
var start = 0
var seen = [Character: Int]()
var matchedCount = 0
var minWindowLength = Int(Int32.max)
// expand the window to match characters
for r in 0..<sChars.count {
var current = sChars[r]
if let freqCount = freq[current] {
seen[current, default: 0] += 1
if seen[current] == freqCount {
matchedCount += 1
}
}
while matchedCount == freq.count {
if (r - l + 1) < minWindowLength {
minWindowLength = r - l + 1
start = l
}
let leftChar = sChars[l]
l += 1
if let freqCount = freq[leftChar] {
if seen[leftChar] == freqCount {
matchedCount -= 1
}
seen[leftChar, default: 0] -= 1
}
}
}
return minWindowLength == Int(Int32.max) ? "" : String(sChars[start..<start + minWindowLength])
}