Construct a string that has exactly K subsequences from given string

Given a string str and an integer K, the task is to find a string S such that it has exactly K subsequences of given string str
Examples: 
 

Input: str = “gfg”, K = 10 
Output: gggggffg 
Explanation: 
There are 10 possible subsequence of the given string “gggggffg”. They are: 
1. gggggffg 
2. gggggffg 
3. gggggffg 
4. gggggffg 
5. gggggffg 
6. gggggffg 
7. gggggffg 
8. gggggffg 
9. gggggffg 
10. gggggffg.
Input: str = “code”, K = 20 
Output: cccccoodde 
Explanation: 
There are 20 possible subsequence of the string “cccccoodde”. 
 

 

Approach:
To solve the problem mentioned above we have to follow the steps given below: 
 

  • The idea is to find the prime factors of K and store the prime factors(say factors).
  • Create an empty array count of size of the given string to stores the count of each character in the resultant string s. Initialize the array with 1.
  • Now pop elements from the list factors and multiply to each position of array in a cyclic way until the list becomes empty. Finally, we have the count of each character of str in the array.
  • Iterate in the array count[] and append the number of characters for each characters ch to the resultant string s.

Below is the implementation of the above approach:
 



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
// Function that computes the string s
void printSubsequenceString(string str,
                            long long k)
{
    // Length of the given string str
    int n = str.size();
    int i;
 
    // List that stores all the prime
    // factors of given k
    vector<long long> factors;
 
    // Find the prime factors
    for (long long i = 2;
         i <= sqrt(k); i++) {
 
        while (k % i == 0) {
            factors.push_back(i);
            k /= i;
        }
    }
    if (k > 1)
        factors.push_back(k);
 
    // Initialize the count of each
    // character position as 1
    vector<long long> count(n, 1);
 
    int index = 0;
 
    // Loop until the list
    // becomes empty
    while (factors.size() > 0) {
 
        // Increase the character
        // count by multiplying it
        // with the prime factor
        count[index++] *= factors.back();
        factors.pop_back();
 
        // If we reach end then again
        // start from beginning
        if (index == n)
            index = 0;
    }
 
    // Store the output
    string s;
 
    for (i = 0; i < n; i++) {
        while (count[i]-- > 0) {
            s += str[i];
        }
    }
 
    // Print the string
    cout << s;
}
 
// Driver code
int main()
{
    // Given String
    string str = "code";
 
    long long k = 20;
 
    // Function Call
    printSubsequenceString(str, k);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
class GFG{
 
// Function that computes the String s
static void printSubsequenceString(String str,
                                      int k)
{
    // Length of the given String str
    int n = str.length();
    int i;
 
    // List that stores all the prime
    // factors of given k
    Vector<Integer> factors = new Vector<Integer>();
 
    // Find the prime factors
    for (i = 2; i <= Math.sqrt(k); i++)
    {
        while (k % i == 0)
        {
            factors.add(i);
            k /= i;
        }
    }
    if (k > 1)
        factors.add(k);
 
    // Initialize the count of each
    // character position as 1
    int []count = new int[n];
    Arrays.fill(count, 1);
    int index = 0;
 
    // Loop until the list
    // becomes empty
    while (factors.size() > 0)
    {
 
        // Increase the character
        // count by multiplying it
        // with the prime factor
        count[index++] *= factors.get(factors.size() - 1);
        factors.remove(factors.get(factors.size() - 1));
 
        // If we reach end then again
        // start from beginning
        if (index == n)
            index = 0;
    }
 
    // Store the output
    String s = "";
 
    for (i = 0; i < n; i++)
    {
        while (count[i]-- > 0)
        {
            s += str.charAt(i);
        }
    }
 
    // Print the String
    System.out.print(s);
}
 
// Driver code
public static void main(String[] args)
{
    // Given String
    String str = "code";
 
    int k = 20;
 
    // Function Call
    printSubsequenceString(str, k);
}
}
 
// This code is contributed by sapnasingh4991

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output: 

cccccoodde



 

Time Complexity: O(N*log2(log2(N))) 
Auxiliary Space: O(K)
 

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.




My Personal Notes arrow_drop_up


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.



Improved By : sapnasingh4991