Longest Sub-string With K Distinct Characters

You are given a string 'S' of length 'N' consisting of lowercase English alphabet letters. You are also given a positive integer 'K'.

Now, a substring of this string is good if it contains at most 'K' distinct characters. A string 'X' is a substring of string 'Y' if it can be obtained by deletion of several continuous elements(possibly zero) from the beginning and the end from the string 'Y'.

Your task is to return the maximum size of any good substring of the string 'S'.

Example:
‘S’ = “bacda” and ‘K’ = 3.

So, the substrings having at most ‘3’ distinct characters are called good substrings. Some possible good substrings are:
1. “bac”
2. “acd”
3. “acda”

The substring “acda” is the largest possible good substring, as we cannot get any other substring of length 5 or more having distinct characters less than or equal to ‘3’. Thus, you should return ‘4’ as the answer.
Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.

The first line of each test case contains a single integer ‘K’, denoting the maximum number of distinct characters.

The second line of each test case contains a string ‘S’
Output Format:
For each test case, print a single line containing an integer denoting the length of the longest substring that contains at most 'K' distinct characters.

The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= K <= 26
1 <= N <= 10^4

All the characters of the string are lowercase English alphabet letters.

Time Limit: 1 sec
CodingNinjas
author
2y
Brute Force
  • We will generate all the substrings using two nested loops and have a ‘check’ function which returns ‘true’ if the number of distinct characters in the substring is less than equal to K, ot...read more
CodingNinjas
author
2y
Improved Version
  • We will run a loop where ‘i’ ranges from ‘0’ to ‘N - 1’ where ‘i’ will be the starting position of the substring and ‘j = i’ to ‘j = N - 1’ where ‘j’ will be the end position of the su...read more
CodingNinjas
author
2y
Using Binary Search
  • We will be doing the binary search on the answer that is the length of the substring.
    • ‘low = 1’ and ‘high = N’ will be the range of our answer. We will have a variable ‘longestLength...read more
CodingNinjas
author
2y
Optimal Approach
  • Create a hash table or a frequency array, and let's name it ‘freq’ and initialize all the positions with 0.
  • We will have a variable ‘start = 0’, which will be the left boundary of the s...read more
Add answer anonymously...
Lifesight Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter