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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Find the String having each substring with exactly K distinct characters
- Count of substrings of length K with exactly K distinct characters
- Minimum length of substring whose rotation generates a palindromic substring
- Count number of substrings with exactly k distinct characters
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Count of distinct characters in a substring by given range for Q queries
- Find distinct characters in distinct substrings of a string
- Longest substring with atmost K characters from the given set of characters
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Minimum length of Run Length Encoding possible by removing at most K characters from a given string
- Length of the longest substring without repeating characters
- Maximum length substring having all same characters after k changes
- Length of longest substring having all characters as K
- Find length of longest substring with at most K normal characters
- String with k distinct characters and no same characters adjacent
- Replace minimal number of characters to make all characters pair wise distinct
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Length of the smallest sub-string consisting of maximum distinct 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 email@example.com. 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.