Construct a string that has exactly K subsequences from given string
Last Updated :
18 May, 2021
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 the size of the given string to store 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 character ch to the resultant string s.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void printSubsequenceString(string str,
long long k)
{
int n = str.size();
int i;
vector< long long > 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);
vector< long long > count(n, 1);
int index = 0;
while (factors.size() > 0) {
count[index++] *= factors.back();
factors.pop_back();
if (index == n)
index = 0;
}
string s;
for (i = 0; i < n; i++) {
while (count[i]-- > 0) {
s += str[i];
}
}
cout << s;
}
int main()
{
string str = "code" ;
long long k = 20;
printSubsequenceString(str, k);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printSubsequenceString(String str,
int k)
{
int n = str.length();
int i;
Vector<Integer> factors = new Vector<Integer>();
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];
Arrays.fill(count, 1 );
int index = 0 ;
while (factors.size() > 0 )
{
count[index++] *= factors.get(factors.size() - 1 );
factors.remove(factors.get(factors.size() - 1 ));
if (index == n)
index = 0 ;
}
String s = "" ;
for (i = 0 ; i < n; i++)
{
while (count[i]-- > 0 )
{
s += str.charAt(i);
}
}
System.out.print(s);
}
public static void main(String[] args)
{
String str = "code" ;
int k = 20 ;
printSubsequenceString(str, k);
}
}
|
Python3
import math
def printSubsequenceString(st, k):
n = len (st)
factors = []
sqt = ( int (math.sqrt(k)))
for i in range ( 2 , sqt + 1 ):
while (k % i = = 0 ):
factors.append(i)
k / / = i
if (k > 1 ):
factors.append(k)
count = [ 1 ] * n
index = 0
while ( len (factors) > 0 ):
count[index] * = factors[ - 1 ]
factors.pop()
index + = 1
if (index = = n):
index = 0
s = ""
for i in range (n):
while (count[i] > 0 ):
s + = st[i]
count[i] - = 1
print (s)
if __name__ = = "__main__" :
st = "code"
k = 20
printSubsequenceString(st, k)
|
C#
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);
}
}
|
Javascript
<script>
function printSubsequenceString(str, k)
{
let n = str.length;
let i;
let factors = new Array();
for (let i = 2;
i <= Math.sqrt(k); i++) {
while (k % i == 0) {
factors.push(i);
k /= i;
}
}
if (k > 1)
factors.push(k);
let count = new Array(n).fill(1);
let index = 0;
while (factors.length > 0) {
count[index++] *= factors[factors.length - 1];
factors.pop();
if (index == n)
index = 0;
}
let s = new String();
for (i = 0; i < n; i++) {
while (count[i]-- > 0) {
s += str[i];
}
}
document.write(s);
}
let str = "code" ;
let k = 20;
printSubsequenceString(str, k);
</script>
|
Time Complexity: O(N*log2(log2(N)))
Auxiliary Space: O(K)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...