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
- Smallest String consisting of a String S exactly K times as a Substring
- Length of the longest subsequence consisting of distinct elements
- Minimum length substring with exactly K distinct characters
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Longest substring consisting of vowels using Binary Search
- Lexicographically smallest K-length substring containing maximum number of vowels
- Maximum length substring having all same characters after k changes
- 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
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Count number of substrings of a string consisting of same characters
- Find distinct characters in distinct substrings of a string
- Longest substring with atmost K characters from the given set of characters
- Count of binary strings of given length consisting of at least one 1
- Length of largest subsequence consisting of a pair of alternating digits