Given 3 positive integers N, M and K. the task is to construct a string of length N consisting of lowercase letters such that each substring of length M having exactly K distinct letters.
Input: N = 5, M = 2, K = 2
Each substring of size 2 “ab”, “ba”, “ad”, “de” have 2 distinct letters.
Input: N = 7, M = 5, K = 3
Each substring of size 5 “tleel”, “leelt”, “eelte” have 3 distinct letters.
Approach: In a string of size N, every substring of size M must contain exactly K distinct letters
- Construct a string having K distinct alphabets starting from ‘a’ to ‘z’ up to the size of M and put the rest of letters like ‘a’..
- Since, we have generated a string of size M with K distinct value. Now, keep repeating it till we reach string size of N.
Below is the implementation of the above approach:
Time complexity: O(N)
Space complexity: O(1)
- Find the String having each substring with exactly K distinct characters
- Generate random String of given size in Java
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Maximum number of set bits count in a K-size substring of a Binary String
- Encrypt string with product of number of vowels and consonants in substring of size k
- Minimum length substring with exactly K distinct characters
- Count of distinct characters in a substring by given range for Q queries
- Generate an array of given size with equal count and sum of odd and even numbers
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Lexicographically smallest and largest substring of size k
- Find distinct characters in distinct substrings of a string
- 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 the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Iterative program to generate distinct Permutations of a String
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Maximum size of square such that all submatrices of that size have sum less than K
- Searching characters and substring in a String in Java
- Longest substring of only 4's from the first N characters of the infinite string
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.