Given a string, find the smallest window length with all distinct characters of the given string. For eg. str = “aabcbcdbca”, then the result would be 4 as of the smallest window will be “dbca” .
Input : aabcbcdbca Output : dbca Explanation : dbca of length 4 is the smallest window with highest number of distinct characters. Input : aaab Output : ab Explanation : ab of length 2 is the smallest window with highest number of distinct characters.
Above problem states that we have to find the smallest window that contains all the distinct characters of the given string even if the smallest string contains repeating elements.
For example, in “aabcbcdb”, the smallest string that contains all the characters is “abcbcd”.
This problem reduces to Find the smallest window in a string containing all characters of another string.
In that problem we find the smallest window that contains all the characters of given pattern.
1- Count all distinct characters in given string.
2- Now follow the algorithm discussed in below post.
We basically 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.
Smallest window containing all distinct characters is dbca
This article is contributed by Sahil Chhabra. 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.
- Python | Get the smallest window in a string containing all characters of given pattern
- Find the smallest window in a string containing all characters of another string
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Swap all occurrences of two characters to get lexicographically smallest string
- Length of the smallest sub-string consisting of maximum distinct characters
- Program to find the largest and smallest ASCII valued characters in a string
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Panalphabetic window in a string
- Make lexicographically smallest palindrome by substituting missing characters
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Minimal moves to form a string by adding characters or appending string itself