Generate a string of size N whose each substring of size M has exactly K distinct characters | Java program to generate a String of size N whose each subString of size M has exactly K distinct characters ; Function to generate the String ; Declare empty String ; counter for M ; counter for K ; Loop to generate String size of N ; Generating K distinct letters one by one ; After generating b distinct letters , append rest a - b letters as ' a ' ; Reset the counter value and repeat the process ; return final result String ; Driver code
import java . util . * ; class GFG { static String generateString ( int N , int M , int K ) { String s = " " ; int cnt1 = 0 ; int cnt2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cnt1 ++ ; cnt2 ++ ; if ( cnt1 <= M ) { if ( cnt2 <= K ) { s = s + ( char ) ( 96 + cnt1 ) ; } else { s = s + ' a ' ; } } else { cnt1 = 1 ; cnt2 = 1 ; s = s + ' a ' ; } } return s ; } public static void main ( String [ ] args ) { int N = 7 , M = 5 , K = 3 ; System . out . println ( generateString ( N , M , K ) ) ; } }