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.

**Examples:**

Input:l = 6, x = 5, y = 3

Output:abcabc

Explanation:

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

Output:aaa

Explanation:

If we take the substring of length 1 then it will have exactly 1 distinct character in it.

**Approach:**

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:

## C++

`// C++ implementation to ` `// construct a string of length L ` `// such that each substring of length ` `// X has exactly Y distinct letters. ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `void` `String(` `int` `l, ` `int` `x, ` `int` `y) ` `{ ` ` ` `// Initialize p equal to the ASCII value of a ` ` ` `int` `p = 97; ` ` ` ` ` `// Iterate till the length of the string ` ` ` `for` `(` `int` `j = 0; j < l ; j++) ` ` ` `{ ` ` ` `char` `ans = (` `char` `)(p + (j % y)); ` ` ` `cout << ans; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main () ` `{ ` ` ` `int` `l = 6; ` ` ` `int` `x = 5; ` ` ` `int` `y = 3; ` ` ` `String(l, x, y) ; ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to ` `// construct a string of length L ` `// such that each substring of length ` `// X has exactly Y distinct letters. ` ` ` `public` `class` `GFG { ` ` ` ` ` `static` `void` `string(` `int` `l, ` `int` `x, ` `int` `y){ ` ` ` ` ` `// Initialize p equal to the ASCII value of a ` ` ` `int` `p = ` `97` `; ` ` ` ` ` `// Iterate till the length of the string ` ` ` `for` `(` `int` `j = ` `0` `; j < l ; j++){ ` ` ` ` ` `char` `ans = (` `char` `)(p + (j % y)); ` ` ` `System.out.print(ans); ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `l = ` `6` `; ` ` ` `int` `x = ` `5` `; ` ` ` `int` `y = ` `3` `; ` ` ` `string(l, x, y) ; ` ` ` `} ` `} ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation to ` `# construct a string of length L ` `# such that each substring of length ` `# X has exactly Y distinct letters. ` ` ` `def` `String(l, x, y): ` ` ` `# Initialize p equal to the ASCII value of a ` ` ` `p ` `=` `97` ` ` ` ` `# Iterate till the length of the string ` ` ` `for` `j ` `in` `range` `(l): ` ` ` ` ` `ans ` `=` `chr` `(p ` `+` `j ` `%` `y) ` ` ` `print` `(ans, end ` `=` `"") ` ` ` ` ` `# Driver code ` `l ` `=` `6` `x ` `=` `5` `y ` `=` `3` `String(l, x, y) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to ` `// construct a string of length L ` `// such that each substring of length ` `// X has exactly Y distinct letters. ` `using` `System; ` ` ` `class` `GFG { ` ` ` `static` `void` `String(` `int` `l, ` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `// Initialize p equal to the ASCII value of a ` ` ` `int` `p = 97; ` ` ` ` ` `// Iterate till the length of the string ` ` ` `for` `(` `int` `j = 0; j < l; j++) ` ` ` `{ ` ` ` `char` `ans = (` `char` `)(p + (j % y)); ` ` ` `Console.Write(ans); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(` `string` `[] args) ` ` ` `{ ` ` ` `int` `l = 6; ` ` ` `int` `x = 5; ` ` ` `int` `y = 3; ` ` ` `String(l, x, y); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

abcabc

## Recommended Posts:

- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Check if the string contains consecutive letters and each letter occurs exactly once
- Find the String having each substring with exactly K distinct characters
- Construct a string that has exactly K subsequences from given string
- Length of the longest substring with no consecutive same letters
- Minimum length substring with exactly K distinct characters
- Check whether a number has exactly three distinct factors or not
- Queries to find whether a number has exactly four distinct factors or not
- Maximized partitions of a string such that each character of the string appears in one substring
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Smallest Greater (than S) String of length K whose letters are subset of S
- Smallest String consisting of a String S exactly K times as a Substring
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Maximum splits in binary string such that each substring is divisible by given odd number
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Minimum number of times A has to be repeated such that B is a substring of it
- Ways to split string such that each partition starts with distinct character

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.