Answers for "generate subsequences of string"

0

generate subsequences of string

// CPP program to generate power set in
// lexicographic order.
#include <bits/stdc++.h>
using namespace std;
 
// str : Stores input string
// n : Length of str.
// curr : Stores current permutation
// index : Index in current permutation, curr
void printSubSeqRec(string str, int n, int index = -1,
                    string curr = "")
{
    // base case
    if (index == n)
        return;
 
    if (!curr.empty()) {
        cout << curr << "\n";
    }
 
    for (int i = index + 1; i < n; i++) {
 
        curr += str[i];
        printSubSeqRec(str, n, i, curr);
 
        // backtracking
        curr = curr.erase(curr.size() - 1);
    }
    return;
}
 
// Generates power set in lexicographic
// order.
void printSubSeq(string str)
{
    printSubSeqRec(str, str.size());
}
 
// Driver code
int main()
{
    string str = "cab";
    printSubSeq(str);
    return 0;
}
Posted by: Guest on April-21-2022

Code answers related to "generate subsequences of string"

Browse Popular Code Answers by Language