Construct string having all possible strings of K letters as subsequence
Last Updated :
04 Mar, 2024
Given two positive integers, let's denote them as N and K. The task is to construct a string S such that all possible strings of length N formed using the first K lowercase English alphabets, appear as subsequences of S.
In case there exist multiple valid solutions, the objective is to output the solution with the minimal length. If there are still several valid possibilities with the same minimal length, any of them can be selected as the output.
Examples:
Input: N = 2, K = 3
Output: abcbac
Explanation: abcbac being the smallest string can be broken down into (aa, ab, ac, ba, bb, bc, ca, cb, cc) which are all the subsequences of given string "abcbac".
Input: N = 3, K = 1
Output: aaa
Explanation: aaa is the smallest string that can be made so that (aaa) occurs in it.
Approach: To solve the problem, follow the below idea:
The minimum length required for the string S is given by the product of N and K (N * K). This ensures that there are enough characters to accommodate all possible subsequences of length N using the initial K lowercase English alphabets.
To construct the string meeting the specified conditions, create a pattern consisting of K different characters (a1, a2, ..., ak) and repeat this pattern N times to form the string.
Step-by-step algorithm:
- Declare a string variable S to store the answer string.
- Run an outer loop for N times.
- Inside the outer loop append first K characters to S.
- After all the iterations, print the final string S.
Below is the implementation of the algorithm:
Java
public class Main {
// Define some constants
static final char nline = '\n';
static final int MOD = 1000000007;
// Function to solve the problem
static void solve() {
int n, k;
n = 2;
k = 3;
StringBuilder s = new StringBuilder();
// Nested loops to construct the desired string
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
// Append characters to the string in a specific pattern
s.append((char)('a' + j));
}
}
// Print the constructed string
System.out.println(s.toString() + nline);
}
// Main function
public static void main(String[] args) {
solve();
}
}
// This code is contributed by shivamgupta310570
C#
using System;
class Program
{
// Main function
static void Main(string[] args)
{
Solve(); // Call the Solve function
}
// Function to solve the problem
static void Solve()
{
int n = 2;
int k = 3;
string s = "";
// Nested loops to construct the desired string
for (int i = 0; i < n; i++)
{
for (int j = 0; j < k; j++)
{
// Append characters to the string in a specific pattern
s += (char)('a' + j);
}
}
// Print the constructed string
Console.WriteLine(s);
}
}
JavaScript
// Javascript program for the above approach
// Define some constants
const nline = '\n';
const MOD = 1000000007;
// Function to solve the problem
function solve(){
let n = 2;
let k = 3;
let s = '';
// Nested loops to construct the desired string
for (let i = 0; i < n; i++) {
for (let j = 0; j < k; j++) {
// Append characters to the string in a specific
// pattern
s += String.fromCharCode('a'.charCodeAt(0) + j);
}
}
// Print the constructed string
console.log(s + nline);
}
// Main function
solve()
// This code is contributed by Susobhan Akhuli
C++14
#include <bits/stdc++.h>
using namespace std;
// Define some constants
#define nline '\n'
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < n; i++)
// Function to solve the problem
void solve()
{
int n, k;
n = 2;
k = 3;
string s;
// Nested loops to construct the desired string
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
// Append characters to the string in a specific
// pattern
s += 'a' + j;
}
}
// Print the constructed string
cout << s << nline;
}
// Main function
signed main() { solve(); }
Python3
# Python Implementation
# Function to solve the problem
def solve():
n = 2
k = 3
s = ""
# Nested loops to construct the desired string
for i in range(n):
for j in range(k):
# Append characters to the string in a specific pattern
s += chr(ord('a') + j)
# Print the constructed string
print(s)
# Main function
if __name__ == "__main__":
solve()
# This code is contributed by Tapesh(tapeshdu420)
Time Complexity: O(N * K)
Auxiliary Space: O(N * K)
Similar Reads
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. g
7 min read
Smallest String consisting of a String S exactly K times as a Substring Given a string S of length N and integer K, find the smallest length string which contains the string S as a sub string exactly K times. Examples: Input: S = "abba", K = 3 Output: abbabbabba Explanation: The string "abba" occurs K times in the string abbabbabba, i.e. {abbabbabba, abbabbabba, abbabba
6 min read
Print all subsequences of a string | Iterative Method Given a string s, print all possible subsequences of the given string in an iterative manner. We have already discussed Recursive method to print all subsequences of a string. Examples: Input : abc Output : a, b, c, ab, ac, bc, abc Input : aab Output : a, b, aa, ab, aab Approach 1 : Here, we discuss
15 min read
Subsequences of given string consisting of non-repeating characters Given a string str of length N, the task is to print all possible distinct subsequences of the string str which consists of non-repeating characters only. Examples: Input: str = "abac" Output: a ab abc ac b ba bac bc c Explanation: All possible distinct subsequences of the strings are { a, aa, aac,
7 min read
All possible strings of any length that can be formed from a given string Given a string of distinct characters, print all possible strings of any length that can be formed from given string characters. Examples: Input: abcOutput: a b c abc ab ac bc bac bca cb ca ba cab cba acbInput: abcdOutput: a b ab ba c ac ca bc cb abc acb bac bca cab cba d ad da bd db abd adb bad bda
10 min read
Number of subsequences as "ab" in a string repeated K times Given a String S, consider a new string formed by repeating the S exactly K times. We need find the number of subsequences as âabâ in the newly formed string. Examples : Input : S = "abcb" K = 2 Output : 6 Here, Given string is repeated 2 times and we get a new string "abcbabcb" Below are 6 occurren
10 min read