Given two integers **N **and **K**. The task is to find the **string of length N**, such that each substring of length greater than equal to **K**, has exactly **K** distinct characters.

**Examples:**

Input:N=10, K=3Output :ABCABCABCAExplanation:The output string has 3 distinct characters.Input :N=20, K=7Output :ABCDEFGABCDEFGABCDEFExplanation:The output string has 7 distinct characters.

**Approach:**

To solve the problem mentioned above the main idea is to print distinct elements up to length **K**, and then repeat the same elements up to **N**.

Below is the implementation of the above approach:

## C++

`// C++ Program to Find the ` `// String having each substring ` `// with exactly K distinct characters ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the ` `// required output string ` `void` `findString(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Each element at index ` ` ` `// i is modulus of K ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` ` ` `cout << ` `char` `(` `'A'` `+ i % K); ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// initialise integers N and K ` ` ` `int` `N = 10; ` ` ` `int` `K = 3; ` ` ` ` ` `findString(N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the ` `// string having each substring ` `// with exactly K distinct characters ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to find the ` `// required output string ` `static` `void` `findString(` `int` `N, ` `int` `K) ` `{ ` ` ` ` ` `// Each element at index ` ` ` `// i is modulus of K ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` `System.out.print((` `char` `)(` `'A'` `+ i % K)); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// Initialise integers N and K ` ` ` `int` `N = ` `10` `; ` ` ` `int` `K = ` `3` `; ` ` ` `findString(N, K); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to Find the ` `# String having each substring ` `# with exactly K distinct characters ` ` ` `# Function to find the ` `# required output string ` `def` `findString(N, K) : ` ` ` `# Each element at index ` ` ` `# i is modulus of K ` ` ` `for` `i ` `in` `range` `(N) : ` ` ` ` ` `print` `(` `chr` `(` `ord` `(` `'A'` `) ` `+` `i ` `%` `K),end` `=` `""); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `# initialise integers N and K ` ` ` `N ` `=` `10` `; ` ` ` `K ` `=` `3` `; ` ` ` ` ` `findString(N, K); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the ` `// string having each substring ` `// with exactly K distinct characters ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function to find the ` `// required output string ` `static` `void` `findString(` `int` `N, ` `int` `K) ` `{ ` ` ` ` ` `// Each element at index ` ` ` `// i is modulus of K ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `Console.Write((` `char` `)(` `'A'` `+ i % K)); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Initialise integers N and K ` ` ` `int` `N = 10; ` ` ` `int` `K = 3; ` ` ` ` ` `findString(N, K); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

ABCABCABCA

**Time Complexity:**O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Minimum length substring with exactly K distinct characters
- Find distinct characters in distinct substrings of a string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Smallest String consisting of a String S exactly K times as a Substring
- Contiguous subsegments of a string having distinct subsequent characters
- Count number of substrings with exactly k distinct characters
- Count of substrings of length K with exactly K distinct characters
- Sub-strings having exactly k characters that have ASCII value greater than p
- String with k distinct characters and no same characters adjacent
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Longest substring having K distinct vowels
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of distinct characters in a substring by given range for Q queries
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Maximum length substring having all same characters after k changes
- Length of longest substring having all characters as K
- Find if string is K-Palindrome or not using all characters exactly once
- Maximized partitions of a string such that each character of the string appears in one substring

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.