#C12930. Longest Distinct Substring and Substring with Exactly K Distinct Characters
Longest Distinct Substring and Substring with Exactly K Distinct Characters
Longest Distinct Substring and Substring with Exactly K Distinct Characters
Problem Statement:
You are given a string and you need to find a specific longest substring. There are two types of queries:
- Type 1: Given a string S, find the longest substring that contains no repeating characters. For example, if S = 'abcabcbb', the answer is 'abc'.
- Type 2: Given a string S and an integer \( k \), find the longest substring that contains exactly \( k \) distinct characters. For example, if S = 'araaci' and \( k = 2 \), the answer is 'araa'.
Input should be read from standard input (stdin) and output should be written to standard output (stdout). Ensure your solution handles all edge cases properly.
inputFormat
The first line contains an integer \( Q \) representing the number of queries. Each of the next \( Q \) lines represents a query in one of the following formats:
- For a Type 1 query:
1 S
(where S is the input string) - For a Type 2 query:
2 S k
(where S is the input string and \( k \) is an integer)
It is guaranteed that the string S contains no spaces.
outputFormat
For each query, output the result on a new line. For a query of type 1, print the longest substring of S with no repeating characters. For a query of type 2, print the longest substring of S that contains exactly \( k \) distinct characters. If no such substring exists, output an empty string.
## sample4
1 abcabcbb
1 pwwkew
2 araaci 2
2 cbbebi 3
abc
wke
araa
cbbeb
</p>