Given a string str and an integer K, the task is to print the length of the longest possible substring that has exactly K unique characters. If there are more than one substring of the longest possible length then print any one of them or print -1 if there is no such substring possible.
Input: str = “aabacbebebe”, K = 3
“cbebebe” is the requried substring.
Input: str = “aabc”, K = 4
Approach: An approach to solve this problem has been discussed in this article. In this article, a binary search based approach will be discussed. Binary search will be applied on the length of the substring which has at least K unique characters. Let’s say we try for length len and check whether a substring of size len is there which is having at least k unique characters. If it is possible then try to maximize the size by searching from this length to the maximum possible length i.e. size of the input string. If it is not possible then search for lower size len.
To check that the length given by binary search will have k unique characters, a set can be used to insert all the characters and then if the size of set is less then k then the answer is not possible else the answer given by the binary search is the max answer.
Binary search is applicable here because it is known if for some len the answer is possible and we want to maximize the len so the search domain changes and we search from this len to n.
Below is the implementation of the above approach:
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.
- Find the longest substring with k unique characters in a given string
- Longest substring consisting of vowels using Binary Search
- Longest substring with atmost K characters from the given set of characters
- Longest substring such that no three consecutive characters are same
- Length of the longest substring without repeating characters
- Print Longest substring without repeating characters
- Longest substring of only 4's from the first N characters of the infinite string
- Find length of longest substring with at most K normal characters
- Longest common substring in binary representation of two numbers
- Longest Common Prefix using Binary Search
- K-th lexicographically smallest unique substring of a given string
- Meta Binary Search | One-Sided Binary Search
- Determine if a string has all Unique Characters
- String with maximum number of unique characters
- Minimize number of unique characters in string
- Count of all unique substrings with non-repeating characters
- Recursive function to do substring search
- Python program to check if a string contains all unique characters
- Largest substring with same Characters
- Why is Binary Search preferred over Ternary Search?
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.