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 email@example.com. 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.
- Maximum count of sub-strings of length K consisting of same characters
- Minimum length substring with exactly K distinct characters
- Maximum length substring having all same characters after k changes
- Lexicographically smallest K-length substring containing maximum number of vowels
- 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
- Count of distinct characters in a substring by given range for Q queries
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Length of longest substring having all characters as K
- Length of the smallest substring which contains all vowels
- 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
- Smallest length string with repeated replacement of two distinct adjacent
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Maximum length substring with highest frequency in a string
- Maximum length palindromic substring such that it starts and ends with given char
- Maximum length palindrome that can be created with characters in range L and R
- Longest substring consisting of vowels using Binary Search