Construct a string that has exactly K subsequences from given string | C # program for the above approach ; Function that computes the String s ; Length of the given String str ; List that stores all the prime factors of given k ; Find the prime factors ; Initialize the count of each character position as 1 ; Loop until the list becomes empty ; Increase the character count by multiplying it with the prime factor ; If we reach end then again start from beginning ; Store the output ; Print the String ; Driver code ; Given String ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void printSubsequenceString ( String str , int k ) { int n = str . Length ; int i ; List < int > factors = new List < int > ( ) ; for ( i = 2 ; i <= Math . Sqrt ( k ) ; i ++ ) { while ( k % i == 0 ) { factors . Add ( i ) ; k /= i ; } } if ( k > 1 ) factors . Add ( k ) ; int [ ] count = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) count [ i ] = 1 ; int index = 0 ; while ( factors . Count > 0 ) { count [ index ++ ] *= factors [ factors . Count - 1 ] ; factors . Remove ( factors [ factors . Count - 1 ] ) ; if ( index == n ) index = 0 ; } String s = " " ; for ( i = 0 ; i < n ; i ++ ) { while ( count [ i ] -- > 0 ) { s += str [ i ] ; } } Console . Write ( s ) ; } public static void Main ( String [ ] args ) { String str = " code " ; int k = 20 ; printSubsequenceString ( str , k ) ; } }