forked from gbrigens/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestSubstringMostTwoDistinctCharacters.swift
40 lines (33 loc) · 1.32 KB
/
LongestSubstringMostTwoDistinctCharacters.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
/**
* Question Link: https://leetcode.com/problems/longest-substring-with-at-most-two-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
*
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class LongestSubstringMostTwoDistinctCharacters {
func lengthOfLongestSubstringTwoDistinct(_ s: String) -> Int {
var start = 0, longest = 0, charFreq = [Character: Int]()
let sChars = Array(s)
for (i, char) in sChars.enumerated() {
if let freq = charFreq[char] {
charFreq[char] = freq + 1
} else {
if charFreq.count == 2 {
longest = max(longest, i - start)
while charFreq.count == 2 {
let charStart = sChars[start]
charFreq[charStart]! -= 1
if charFreq[charStart] == 0 {
charFreq[charStart] = nil
}
start += 1
}
}
charFreq[char] = 1
}
}
return max(longest, sChars.count - start)
}
}