forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestSubstringMostKDistinctCharacters.swift
41 lines (34 loc) · 1.3 KB
/
LongestSubstringMostKDistinctCharacters.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
/**
* Question Link: https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/
* Primary idea: Slding window, use dictionary to check substring is valid or not, and
note to handle the end of string edge case
*
* Note: k may be invalid, mention that with interviewer
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class LongestSubstringMostKDistinctCharacters {
func lengthOfLongestSubstringKDistinct(_ s: String, _ k: Int) -> Int {
guard k > 0 else {
return 0
}
let s = Array(s)
var start = 0, longest = 0, charsFreq = [Character: Int]()
for (i, char) in s.enumerated() {
if let freq = charsFreq[char] {
charsFreq[char] = freq + 1
} else {
while charsFreq.count == k {
longest = max(i - start, longest)
guard let freq = charsFreq[s[start]] else {
fatalError()
}
charsFreq[s[start]] = freq == 1 ? nil : freq - 1
start += 1
}
charsFreq[char] = 1
}
}
return max(longest, s.count - start)
}
}