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.
Input: N=10, K=3 Output : ABCABCABCA Explanation: The output string has 3 distinct characters. Input : N=20, K=7 Output : ABCDEFGABCDEFGABCDEF Explanation: The output string has 7 distinct characters.
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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find distinct characters in distinct substrings of a string
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Find the longest substring with k unique characters in a given string
- Find the number of strings formed using distinct characters of a given string
- Minimum length substring with exactly K distinct characters
- Count of distinct characters in a substring by given range for Q queries
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- String with k distinct characters and no same characters adjacent
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Searching characters and substring in a String in Java
- Longest substring of only 4's from the first N characters of the infinite string
- Convert given string so that it holds only distinct characters
- Print all distinct characters of a string in order (3 Methods)
- Check whether count of distinct characters in a string is Prime or not
- Contiguous subsegments of a string having distinct subsequent characters
- Minimum steps to delete a string by deleting substring comprising of same characters
- Length of the smallest sub-string consisting of maximum distinct characters
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Find length of longest substring with at most K normal characters
- String Range Queries to count number of distinct characters with updates
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.