Skip to content

Commit

Permalink
[Array] Add a solution to Longest Substring with At Most Two Distinct…
Browse files Browse the repository at this point in the history
… Characters
  • Loading branch information
soapyigu committed Apr 20, 2018
1 parent 13476a6 commit ffdc1c0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
40 changes: 40 additions & 0 deletions Array/LongestSubstringMostTwoDistinctCharacters.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
}
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[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).
[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).


## Array
Expand Down Expand Up @@ -70,9 +70,11 @@
[Game of Life](https://leetcode.com/problems/game-of-life/)| [Swift](./Array/GameLife.swift)| Medium| O(n)| O(1)|
[Task Scheduler](https://leetcode.com/problems/task-scheduler/)| [Swift](./Array/TaskScheduler.swift)| Medium| O(nlogn)| O(n)|
[Sliding Window Maximum ](https://leetcode.com/problems/sliding-window-maximum/)| [Swift](./Array/SlidingWindowMaximum.swift)| Hard| O(n)| O(n)|
[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)|
[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [Swift](./Array/LongestConsecutiveSequence.swift)| Hard| O(n)| O(n)|



## String
| Title | Solution | Difficulty | Time | Space |
| ----- | -------- | ---------- | ---- | ----- |
Expand Down Expand Up @@ -633,7 +635,7 @@
| [Swift](./Search/FindPeakElement.swift) | 162 | [Find Peak Element](https://oj.leetcode.com/problems/find-peak-element/) | Medium |
| [Swift](./String/OneEditDistance.swift) | 161 | [One Edit Distance](https://oj.leetcode.com/problems/one-edit-distance/)♥ | Medium |
| | 160 | [Intersection of Two Linked Lists](https://oj.leetcode.com/problems/intersection-of-two-linked-lists/) | Easy |
| | 159 | [Longest Substring with At Most Two Distinct Characters](https://oj.leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) ♥ | Hard |
| [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 |
| | 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 |
| | 157 | [Read N Characters Given Read4](https://oj.leetcode.com/problems/read-n-characters-given-read4/) ♥ | Easy |
| [Swift](./Tree/BinaryTreeUpsideDown) | 156 | [Binary Tree Upside Down](https://oj.leetcode.com/problems/binary-tree-upside-down/) ♥ | Medium |
Expand Down

0 comments on commit ffdc1c0

Please sign in to comment.