Coding Problem 7
Longest Substring
asked by Amazon
Ques. Given an integer K and string S, find the length of the longest substring that contains at most K distinct characters. For example, given S = "abcba" and K = 2 , the longest substring with K distinct characters is "bcb".
Solution.
This problem can be solved by many methods but time complexity will be high. There is a solution of O(n) time complexity. We can use the method of sliding window here. In the window it will contain the substring and it will be stable if it contains K distinct characters at any point. If window contains less than K charcaters then we expand the windw by adding more characters from the right side otherwise if window contains more than K distinct charcaters we will shrink the window by deleting the characters from left side of window. We will continue the same process untill we reach the last characters from the string. At the time of window moving we can take care of longest substring size also.We gonna need the list or set to keep the frequencies of characters in the string becuase a charcater can occur many times. So if we add the character into window we add the count of frequency and if we remove it from the window then we will descrease the count. To keep the frequency we are gonna need the long array or set which will keep the track of count at the index equal to ASCII value of charcater. It will make easy for us to access the frequency of character. ord() function will be used to access the ASCII value of charcater. Lets see the code now -
def longestSubstr(s, k):
end = begin = 0 # boundaries of substring
window = set() # store characters
freq = [0]*256 # store the frequency of char currently in the window
low = high = 0 # sliding window boundaries
while high < len(s):
window.add(s[high])
freq[ord(s[high])] += 1
while len(window) > k:
freq[ord(s[low])] -= 1
if freq[ord(s[low])] == 0:
window.remove(s[low])
low += 1
# check if substring size is increased then update the value
if end-begin < high-low:
end = high
begin = low
high += 1
return s[begin:end + 1]
s = 'abbcddba'
k = 2
print(longestSubstr(s, k))
Output : "abb"
Note : There can be multiple answers for a string just like above case, we have print out any one of them.
Comments
Post a Comment