-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsequences of string
46 lines (34 loc) · 989 Bytes
/
subsequences of string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
input : "abc" is a string
output : ["","a","b","c","ab","bc","ac","abc"]
returing all the subsequences of input string.
*/
#include<iostream>
using namespace std;
int subs(string input, string output[]){
// base case
if(input.empty()){ // .empty check for variable present or not
output[0] = "";
return 1;
}
string smallString = input.substr(1);
// recursion
int smallOutputSize = subs(smallString,output);
// small calculation
for(int i = 0; i < smallOutputSize; i++){
output[i + smallOutputSize] = input[0] + output[i];
}
return 2*smallOutputSize;
}
int main(){
string input;
cin >> input;
// dynamically made the o/p string
string* output = new string[1000];
// storing the o/p value no. to count variable
int count = subs(input,output);
// print the o/p array for count
for(int i =0; i < count; i++){
cout << output[i] << endl;
}
}