0% completed
Solution: Longest Substring with K Distinct Characters
Problem Statement
Given a string, find the length of the longest substring in it with no more than K distinct characters.
You can assume that K is less than or equal to the length of the given string.
Example 1:
Input: str="araaci", K=2
Output: 4
Explanation: The longest substring with no more than '2' distinct characters is "araa".
Example 2:
Input: str="araaci", K=1
Output: 2
Explanation: The longest substring with no more than '1' distinct characters is "aa".
Example 3:
Input: str="cbbebi", K=3
Output: 5
.....
.....
.....
Luis Roel
· 2 years ago
def longestSubstring(str1, k): l = 0 max_len = 0 counts = {} for r in range(len(str1)): # Add in an item to the window, by adding 1 to its count counts[str1[r]] += counts.get(str1[r], 0) + 1 # Now we contract the window if we've seen more than # 'k' characters while len(counts) > k: # Take an item out of the window by subtracting # 1 from its ounts counts[str1[l]] -= 1 # If it turns out that we've exhausted this key, # delete it from the dict if counts[str1[l]] == 0: del counts[str1[l]] # Then shift our pointer to the right l += 1 max_len = max(max_len, r - l + 1) return max_len
Mohammed Dh Abbas
· 2 years ago
class Solution: def findLength(self, text, k): max_length = float('-inf') freq = {} j = 0 for i in range(len(text)): freq[text[i]] = freq.get(text[i], 0) freq[text[i]] += 1 if len(freq) <= k: max_length = max(max_length, i - j + 1) else: while len(freq) > k: freq[text[j]] -= 1 if freq[text[j]] == 0: del freq[text[j]] j += 1 return max_length
Ariel Davies
· 2 years ago
The current solution uses while(Object.keys()) which increases the time complexity from O(N) to O(N * K) where K is each key in the object that has to be added again into an array for each iteration.
The optimal solution would instead have an int as the counter which gets us back to O(N).
arya.javadi80
· 3 years ago
First of all, If you add a else condition for adding the characters to hashmap, you encounter an error which doesn't make sense, and second why do you initialize the char frequency to a 0 instead of a 1
Rohit Sharma
· 3 years ago
Using set will make code more cleaner
Sourav Rawat
· 4 years ago
This is one that is causing me a lot of trouble https://leetcode.com/problems/sliding-window-maximum/
Ngân Nguyễn
· 4 years ago
I didn't really get the window " In the current window, ...." . Can anyone explain that? Tks in advance.
Anthony DiFede
· 4 years ago
I believe I accounted for the edge cases...
As long as you encounter a letter you've seen before, make the window start = window end since you will only be caring about a substring with no duplicates.

Anthony DiFede
· 4 years ago
Can someone give me some feedback on this?

Viacheslav Lushchinskiy
· 4 years ago
As discussed below here is the solution similar to that in example but with if instead of while
const f = (_str, _k) => { let windowStart = 0; const maxArray = []; const charFrequency = {};
for (let windowEnd = 0; windowEnd < _str.length; windowEnd++) { const rightChar = _str[windowEnd]; maxArray.push(rightChar);
if (!(rightChar in charFrequency)) { charFrequency[rightChar] = 0; } charFrequency[rightChar] += 1;
if (Object.keys(charFrequency).length > _k) { maxArray.shift(); // removes the first element in the Array const leftChar = _str[windowStart]; charFrequency[leftChar] -= 1; if (charFrequency[leftChar] === 0) { delete charFrequency[leftChar]; } windowStart += 1; } } return maxArray.length; };
Reading Progress
0%