Write a program that accepts input from user and print all the subsequences of that string.
Example:
Input: abcd Output: : a ab abcabcdabd ac acd ad b bcbcd bd c cd d
Algorithm:
Time Complexity: O(2n)
Code
#include <iostream> #include <string> using namespace std; void printSubsequences(string str, int start, int end, string curStr = ""){ //base case if (start == end) { return; } //print current string permutation cout<<curStr<< " "; for (int i = start + 1; i< end; i++) { curStr += str[i]; printSubsequences(str, i, end, curStr); curStr = curStr.erase(curStr.size() - 1); } } int main(){ string s; cout<< "Enter string : "; cin>> s; int len = s.size(); cout<< "Subsequences : "; printSubsequences(s, -1, len); return 0; }
Output
Enter string : abcd Subsequences : a ab abcabcdabd ac acd ad b bcbcd bd c cd d
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Algorithm:
Fix character in curStr and prints the string
Time Complexity: O(2n)
Code
Output