Given the length of the string l, the length of the substring x and the number of distinct characters that a substring of length x must have are y, the task is to find a string of length l in which every substring of length x has y distinct characters.
Input: l = 6, x = 5, y = 3
If we take a substring of the first five characters then the substring will be “abcab”.
There are exactly three distinct characters (a, b, c) in the substring.
Similarly, if we take any substring from the string of length 5 then it will have
exactly 3 distinct characters.
Input: l = 3, x = 1, y = 1
If we take the substring of length 1 then it will have exactly 1 distinct character in it.
To solve the problem mentioned above we follow the steps given below:
- Initialize a variable p as 97 which marks the ASCII value of lower case letter ‘a’.
- Keep incrementing the value of p till y characters.
- Then repeat the character from the first character till it’s length become equal to length of given string and return the final string.
Below is the implementation of the above approach:
- Length of the longest substring with no consecutive same letters
- Smallest Greater (than S) String of length K whose letters are subset of S
- Minimum length substring with exactly K distinct characters
- Find length of longest subsequence of one string which is substring of another string
- Convert to a string that is repetition of a substring of k length
- Maximum length substring with highest frequency in a string
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Smallest length string with repeated replacement of two distinct adjacent
- Length of the smallest sub-string consisting of maximum distinct characters
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Maximum length substring having all same characters after k changes
- Find max length odd parity substring
- Length of the longest substring that do not contain any palindrome
- Length of the smallest substring which contains all vowels
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest substring with equal 1s and 0s
- Length of the longest valid substring
- Length of longest substring having all characters as K
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01