Given a string of length N, find the length of the smallest sub-string consisting of maximum distinct characters. Note : Our output can have same character.
Input : "AABBBCBB" Output : 5 Input : "AABBBCBBAC" Output : 3 Explanation : Sub-string -> "BAC" Input : "GEEKSGEEKSFOR" Output : 8 Explanation : Sub-string -> "GEEKSFOR"
Method 1 (Brute Force)
We can consider all sub-strings one by one and check for each sub-string both conditions together
1. sub-string’s distinct characters is equal to maximum distinct characters
2. sub-sting’s length should be minimum .
Time Complexity : O(n^3)
The length of the smallest substring consisting of maximum distinct characters : 5
Method 2 (Efficient)
- Count all distinct characters in given string.
- Maintain a window of characters. Whenever the window contains all characters of given string, we shrink the window from left side to remove extra characters and then compare its length with smallest window fount so far.
Please refer Smallest window that contains all characters of string itself for implementation and more details.
Asked In : DailyHunt
This article is contributed by Harshit Agrawal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Smallest String consisting of a String S exactly K times as a Substring
- Maximum count of sub-strings of length K consisting of same characters
- Minimum length substring with exactly K distinct characters
- Length of the longest subsequence consisting of distinct elements
- Length of longest subsequence consisting of distinct adjacent elements
- Partition array into minimum number of equal length subsets consisting of a single distinct value
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Lexicographically smallest K-length substring containing maximum number of vowels
- Maximum length substring having all same characters after k changes
- Longest substring consisting of vowels using Binary Search
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- 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 distinct characters in a substring by given range for Q queries
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Longest substring with atmost K characters from the given set of characters
- Find distinct characters in distinct substrings of a string
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions