Raju is preparing questions for the internal test. While creating questions, he gave one such question to his son Arun to solve. Raju has given the string S to his son Arun. Arun's task is to print the size of the longest possible substring that has exactly K unique characters.ExampleInput:String = aabacbebebe; K = 3Output:The substring will be cbebebe with length 7. Explanation:The unique characters in the string are: 'a', 'b', 'c', and 'e'. The longest subsequence with at most 3 distinct characters is "aabacbe". It contains the characters 'a', 'b', and 'c', which is the maximum number of distinct characters allowed (k = 3). Therefore, the length of the longest subsequence is 7.Input format :The first line of input consists of the string S.The second line of input consists of the integer K.Output format :The output prints the size of the longest possible substring that has exactly K unique characters.Refer to the sample output for the formatting specifications.Code constraints :1≤ string length ≤ 501≤ K ≤10Sample test cases :Input 1 :aabacbebebe3Output 1 :7Input 2 :abbc1Output 2 :2
Question
Raju is preparing questions for the internal test. While creating questions, he gave one such question to his son Arun to solve. Raju has given the string S to his son Arun. Arun's task is to print the size of the longest possible substring that has exactly K unique characters.ExampleInput:String = aabacbebebe; K = 3Output:The substring will be cbebebe with length 7. Explanation:The unique characters in the string are: 'a', 'b', 'c', and 'e'. The longest subsequence with at most 3 distinct characters is "aabacbe". It contains the characters 'a', 'b', and 'c', which is the maximum number of distinct characters allowed (k = 3). Therefore, the length of the longest subsequence is 7.Input format :The first line of input consists of the string S.The second line of input consists of the integer K.Output format :The output prints the size of the longest possible substring that has exactly K unique characters.Refer to the sample output for the formatting specifications.Code constraints :1≤ string length ≤ 501≤ K ≤10Sample test cases :Input 1 :aabacbebebe3Output 1 :7Input 2 :abbc1Output 2 :2
Solution
To solve this problem, we can use the sliding window technique. Here are the steps:
- Initialize two pointers,
startandend, to the beginning of the string. - Create a dictionary,
char_count, to keep track of the count of each character in the current substring. - Iterate over the string using the
endpointer. For each character:- Add the character to
char_count. - If the number of unique characters in
char_countis greater thanK, move thestartpointer to the right until the number of unique characters is exactlyK.
- Add the character to
- After each iteration, update the maximum length of the substring if the current substring is longer.
- Continue this process until the
endpointer reaches the end of the string. - The maximum length recorded is the length of the longest substring with exactly
Kunique characters.
Here is a Python code snippet that implements this algorithm:
def longest_substring(s, k):
char_count = {}
start = 0
max_length = 0
for end in range(len(s)):
if s[end] not in char_count:
char_count[s[end]] = 0
char_count[s[end]] += 1
while len(char_count) > k:
char_count[s[start]] -= 1
if char_count[s[start]] == 0:
del char_count[s[start]]
start += 1
max_length = max(max_length, end - start + 1)
return max_length
You can call this function with the string and K as arguments to get the length of the longest substring with exactly K unique characters. For example:
print(longest_substring('aabacbebebe', 3)) # Output: 7
print(longest_substring('abbc', 1)) # Output: 2
This function works by maintaining a sliding window of characters that contains at most K unique characters, and continuously updates the maximum length of such a substring as it iterates over the string.
Similar Questions
Given a string s, find the length of the longest substring without repeating characters. Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Example 2:Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1.Example 3:Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Constraints:0 <= s.length <= 5 * 104s consists of English letters, digits, symbols and spaces.
Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1:Input:S = "aba", K = 2Output:3Explanation:The substrings are: "ab", "ba" and "aba".
Given two integers N and K, the task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string should be from integers ranging from 0 to K-1. Example 1:Input:N = 2, K = 2Output: 00110Explanation: Allowed characters are from 0 to k-1 (i.e., 0 and 1).There are 4 string possible of size N=2 (i.e "00", "01","10","11")"00110" contains all possible string as a substring. It also has the minimum length.Example 2:Input:N = 2, K = 3Output: 0010211220Explanation: Allowed characters are from 0 to k-1 (i.e., 0, 1 and 2).There are total 9 strings possibleof size N, given output string has the minimumlength that contains all those strings as substring.Your Task: You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.Expected Time Complexity: O(KNlogK)Expected Space Complexity: O(KNN)Constraints:1 ≤ N ≤ 41 < K < 101 < KNN < 106
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.