Q:

You are given a string, you have to print all the subsequences of the string in lexicographical order

0

Print all subsequences of a string

You are given a string, you have to print all the subsequences of the string in lexicographical order.

Problem description:

The above problem wants you to print all possible arrangement of the string, that is using string length 1 to maximum valid length and also the string should be in lexicographical order which means according to dictionary appearance or sorted sequence.

Input:

The first line of the input consist of T number of test cases, each test case consist of a string str.

Output:

You need to print all the subsequences of the given string int lexicographical order separated by new line.

Examples:

Input:
T=1
str="aa"

Output:
a
a
aa

Input:
T=1
str="abc"

Output:
a
ab
abc
ac
b	
bc
c

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

To solve this problem we will use the backtracking concept along with multiset which will store the subsequences in lexicographical order.

For each character in the given string, it has two options either it will be in the subsequence or will not be in the subsequence, because for this reason, we need to backtrack the solution.

Pseudo Code:

  1. We declare a solve function.
  2. It takes two parameters as a string (str) and position (pos).
  3. The pos will be the index from where we will start the insertion of character in temporary string temp.
  4. Then we will again call solve function and add 1 to current index and recursively move forward.
  5. Once we reached the maximum index in recursive calls we will backtrack the current index from where recursion started and pop the character which was inserted in temp string initially.
  6. During each call of the solve function, we will insert the temporary string in the multiset.

Time Complexity for the above approach in the worst case is exponential.

C++ Implementation:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

//temporary string.
string temp;

//multiset for maintaining
//lexicographical order.
multiset<string> st;

//solve function which will make
//recursive calls
void solve(string str, ll pos)
{
    //insert in multiset.
    st.insert(temp);

    //iterate through all characters.
    for (ll i = pos; i < str.length(); i++) {
        //insert current character.
        temp.push_back(str[i]);
        //make recursive call.
        solve(str, i + 1);
        //backtrack
        temp.pop_back();
    }
}

int main()
{
    cout << "Enter number of test cases: ";
    int t;
    cin >> t;

    while (t--) {
        st.clear();
        cout << "Enter string: ";
        string str;
        cin >> str;

        temp = "";
        solve(str, 0);

        cout << "Possible Subsequences in lexicographical order:";
        for (auto it = st.begin(); it != st.end(); it++)
            cout << *it << "\n";
    }

    return 0;
}

Output:

Enter number of test cases: 3
Enter string: ab
Possible Subsequences in lexicographical order:
a
ab
b
Enter string: abc
Possible Subsequences in lexicographical order:
a
ab
abc
ac
b
bc
c
Enter string: aa 
Possible Subsequences in lexicographical order:
a
a
aa

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

interview C++ coding problems/challenges | Backtracking

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
You are given a matrix of size N*M, you start from... >>
<< The n-queens puzzle is the problem of placing n qu...