Skip to content

Commit ffdc1c0

Browse files
committed
[Array] Add a solution to Longest Substring with At Most Two Distinct Characters
1 parent 13476a6 commit ffdc1c0

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/
3+
* Primary idea: Slding window, use dictionary to check substring is valid or not, and
4+
note to handle the end of string edge case
5+
*
6+
* Time Complexity: O(n), Space Complexity: O(n)
7+
*
8+
*/
9+
10+
class LongestSubstringMostTwoDistinctCharacters {
11+
func lengthOfLongestSubstringTwoDistinct(_ s: String) -> Int {
12+
var start = 0, longest = 0, charFreq = [Character: Int]()
13+
let sChars = Array(s)
14+
15+
for (i, char) in sChars.enumerated() {
16+
if let freq = charFreq[char] {
17+
charFreq[char] = freq + 1
18+
} else {
19+
if charFreq.count == 2 {
20+
longest = max(longest, i - start)
21+
22+
while charFreq.count == 2 {
23+
let charStart = sChars[start]
24+
charFreq[charStart]! -= 1
25+
26+
if charFreq[charStart] == 0 {
27+
charFreq[charStart] = nil
28+
}
29+
30+
start += 1
31+
}
32+
}
33+
34+
charFreq[char] = 1
35+
}
36+
}
37+
38+
return max(longest, sChars.count - start)
39+
}
40+
}

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 235 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 236 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -70,9 +70,11 @@
7070
[Game of Life](https://leetcode.com/problems/game-of-life/)| [Swift](./Array/GameLife.swift)| Medium| O(n)| O(1)|
7171
[Task Scheduler](https://leetcode.com/problems/task-scheduler/)| [Swift](./Array/TaskScheduler.swift)| Medium| O(nlogn)| O(n)|
7272
[Sliding Window Maximum ](https://leetcode.com/problems/sliding-window-maximum/)| [Swift](./Array/SlidingWindowMaximum.swift)| Hard| O(n)| O(n)|
73+
[Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/)| [Swift](./Array/LongestSubstringMostTwoDistinctCharacters.swift)| Hard| O(n)| O(n)|
7374
[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [Swift](./Array/LongestConsecutiveSequence.swift)| Hard| O(n)| O(n)|
7475

7576

77+
7678
## String
7779
| Title | Solution | Difficulty | Time | Space |
7880
| ----- | -------- | ---------- | ---- | ----- |
@@ -633,7 +635,7 @@
633635
| [Swift](./Search/FindPeakElement.swift) | 162 | [Find Peak Element](https://oj.leetcode.com/problems/find-peak-element/) | Medium |
634636
| [Swift](./String/OneEditDistance.swift) | 161 | [One Edit Distance](https://oj.leetcode.com/problems/one-edit-distance/)♥ | Medium |
635637
| | 160 | [Intersection of Two Linked Lists](https://oj.leetcode.com/problems/intersection-of-two-linked-lists/) | Easy |
636-
| | 159 | [Longest Substring with At Most Two Distinct Characters](https://oj.leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) ♥ | Hard |
638+
| [Swift](./Array/LongestSubstringMostTwoDistinctCharacters.swift) | 159 | [Longest Substring with At Most Two Distinct Characters](https://oj.leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) ♥ | Hard |
637639
| | 158 | [Read N Characters Given Read4 II - Call multiple times](https://oj.leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) ♥ | Hard |
638640
| | 157 | [Read N Characters Given Read4](https://oj.leetcode.com/problems/read-n-characters-given-read4/) ♥ | Easy |
639641
| [Swift](./Tree/BinaryTreeUpsideDown) | 156 | [Binary Tree Upside Down](https://oj.leetcode.com/problems/binary-tree-upside-down/) ♥ | Medium |

0 commit comments

Comments
 (0)