Given a string S and a number K. The task is to find the minimum length substring having exactly K distinct characters.
Note: The string S consists of only lowercase English alphabets.
Input: S = "ababcb", K = 3 Output: abc Input: S="efecfefd", K = 4 Output: cfefd
Simple Solution: simple solution is to consider each substring and check if it contains k distinct characters. If yes then compare the length of this substring with minimum length substring found earlier. Time complexity of this approach is O(N2), where N is the length of the String S.
Efficient Solution: An efficient solution is to use sliding window technique and hashing. The idea is to use two pointers st and end to denote starting and ending point of sliding window. Initially point both to beginning of the string. Move end forward and increment count of corresponding character. If count is one then a new distinct character is found and increment count of number of distinct characters. If count of number of distinct characters is greater than k then move st forward and decrease count of character. If character count is zero then a distinct character is removed and count of distinct elements can be reduced to k this way. If count of distinct elements is k, then remove characters from beginning of sliding window having count greater than 1 by moving st forward. Compare length of current sliding window with minimum length found so far and update if necessary.
Note that each character is added and removed from sliding window at most once, so each character is traversed twice. Hence the time complexity is linear.
Below is the implementation of above approach:
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
- Maximum length substring having all same characters after k changes
- Length of the longest substring without repeating characters
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Length of the smallest sub-string consisting of maximum distinct characters
- Minimum characters to be replaced to remove the given substring
- Minimum K such that every substring of length atleast K contains a character c
- Minimum steps to delete a string by deleting substring comprising of same characters
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Find distinct characters in distinct substrings of a string
- Replace minimal number of characters to make all characters pair wise distinct
- String with k distinct characters and no same characters adjacent
- Longest substring having K distinct vowels
- Count number of Distinct Substring in a String
- Minimum number of operations to move all uppercase characters before all lower case characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.