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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Find the String having each substring with exactly K distinct characters
- Minimum length of substring whose rotation generates a palindromic substring
- Count of distinct characters in a substring by given range for Q queries
- Maximum length substring having all same characters after k changes
- Length of the longest substring without repeating characters
- Find length of longest substring with at most K normal characters
- Count of substrings of length K with exactly K distinct 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
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- 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
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Longest substring with atmost K characters from the given set of 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.