Skip to content

Commit dd0000c

Browse files
author
Partho Biswas
committed
340_Longest_Substring_with_At_Most_K_Distinct_Characters
1 parent b87d410 commit dd0000c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ I have solved quite a number of problems from several topics. See the below tabl
218218
|09| [344. Reverse String](https://leetcode.com/problems/reverse-string/)| [Python](https://tinyurl.com/wu6rdaw/344_Reverse_String.py)|
219219
|10| [482. License Key Formatting](https://leetcode.com/problems/license-key-formatting/)| [Python](https://tinyurl.com/wu6rdaw/482_License_Key_Formatting.py)| |
220220
|11| [3. Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| [Python](https://tinyurl.com/wu6rdaw/3_Longest_Substring_Without_Repeating_Characters.py)| [Vid 1](https://tinyurl.com/tbwjwcf), [Vid 2](https://www.youtube.com/watch?v=3IETreEybaA) | Medium | 📌 Sliding window, Two pointer |
221-
|12| [340. Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/)| [Python](https://tinyurl.com/wu6rdaw/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py)| [Vid 1](https://tinyurl.com/t55ef99), [Art 1](https://tinyurl.com/vxumhku), [Art 2](https://tinyurl.com/ukyev7f) | Hard | 📌 Sliding window, Two pointer |
221+
|12| [340. Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/)| [Python](https://tinyurl.com/wu6rdaw/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py), [Swift](https://tinyurl.com/wuja3c4/340_Longest_Substring_with_At_Most_K_Distinct_Characters.swift) | [Vid 1](https://tinyurl.com/t55ef99), [Art 1](https://tinyurl.com/vxumhku), [Art 2](https://tinyurl.com/ukyev7f) | Hard | 📌 Sliding window, Two pointer |
222222
|13| [159. Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/)| [Python](https://tinyurl.com/wu6rdaw/159_Longest_Substring_with_At_Most_Two_Distinct_Characters.py)| [Vid 1](https://tinyurl.com/t55ef99) | Hard | 📌 Sliding window, Two pointer |
223223
|14| [424. Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/)| [Python](https://tinyurl.com/wu6rdaw/424_Longest_Repeating_Character_Replacement.py)| **[Vid 1](https://tinyurl.com/svyns7x)**, [Vid 2](https://tinyurl.com/rj3yt25), [Art 1](https://tinyurl.com/yx34cd9c), [Art 2](https://tinyurl.com/suxeoj3), [Art 3](https://tinyurl.com/slpeqnd), [Art 4](https://tinyurl.com/sje2no8) | Medium | 📌 Sliding window, Very important |
224224
|15| **[76. Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)** | [Python](https://tinyurl.com/wu6rdaw/76_Minimum_Window_Substring.py)| **[Vid 1](https://tinyurl.com/tv8lqpa)**, **[Vid 2](https://tinyurl.com/v38h4j4)**, **[Vid 3](https://tinyurl.com/ss5ue49)** | Hard | 📌 Sliding window, Very important |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Foundation
2+
class Solution {
3+
func lengthOfLongestSubstringKDistinct(_ s: String, _ k: Int) -> Int {
4+
guard k > 0 else {
5+
return 0
6+
}
7+
var sArr = Array(s)
8+
var counter = [Character:Int]()
9+
var maxLen = 0
10+
var (left, right) = (0, 0)
11+
while right < sArr.count {
12+
if counter[sArr[right]] != nil || counter.keys.count < k {
13+
counter[sArr[right], default: 0] += 1
14+
maxLen = max(maxLen, right - left + 1)
15+
right += 1
16+
} else {
17+
counter[sArr[left], default: 0] -= 1
18+
if counter[sArr[left]] == 0 {
19+
counter.removeValue(forKey: sArr[left])
20+
}
21+
left += 1
22+
}
23+
}
24+
return maxLen
25+
}
26+
}

0 commit comments

Comments
 (0)