top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a C/C++ program to print all possible strings of length k that can be formed from a set of n characters?

+2 votes
1,600 views

Input:
arr = {'a', 'b'}, length = 3

Output:
aaa
aab
aba
abb
baa
bab
bba
bbb

posted Mar 29, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Go through this code -:
using namespace std;

int K;

void printallpossible(char arr[], int N, string res, int k)
{
if (k == K)
{
cout << res << endl;
return;
}
for (int i = 0; i < N; i++)
{
res += arr[i];
printallpossible(arr, N, res, k + 1);
res.erase(res.end() - 1);
}
}

int main()
{
char arr[] = {'a', 'b'};
int N = sizeof(arr) / sizeof(char);
K = 3;
printallpossible(arr, N, "", 0);
}

answer Apr 28, 2017 by Naziya Raza Khan
Similar Questions
0 votes

Given a string S and set of strings U. Find the length of longest prefix of S that can be formed using strings in U.

For example you have following -

S= “absolute”
U={“abs”,”xyz”,”ol”,”te”}

Answer: 5 i.e "absol"

Now the task is to have the algorithm/program for the above problem, please help me with this?

+2 votes

Write a C program which accept two strings and print characters in second string which are not present in first string?

Example:
String 1: apple
String 2: aeroplane

output:
ron

+2 votes

Given a set of random strings, write a function that returns a set that groups all the anagrams together.

Ex: star, dog, car, rats, arc - > {(star, rats), (arc,car), dog}

...