#include <iostream>
#include <string>
// Recursive function to print all subsequences of the given string
void printSubsequences(const std::string &str, int index, std::string current) {
// Base case: If we've processed all characters of the string
if (index == str.size()) {//imp
// Print the current subsequence if it's not empty
if (!current.empty()) {
std::cout << current << std::endl;
}
return; // Return to explore other subsequences
}
// Recursively call to include the current character in the subsequence
printSubsequences(str, index + 1, current + str[index]);
// Recursively call to exclude the current character from the subsequence
printSubsequences(str, index + 1, current);
}
int main() {
std::string str = "abc"; // Input string
printSubsequences(str, 0, ""); // Initial call with an empty current subsequence
return 0;
}
/*
index == str.size()
so print only index value 3
and include and exclude one after one
How it Works Step-by-Step for "abc"
Start with an empty subsequence: printSubsequences("abc", 0, "").
Include 'a': printSubsequences("abc", 1, "a").
Include 'b': printSubsequences("abc", 2, "ab").
Include 'c': printSubsequences("abc", 3, "abc") → Print "abc".
Exclude 'c': printSubsequences("abc", 3, "ab") → Print "ab".
Exclude 'b': printSubsequences("abc", 2, "a").
Include 'c': printSubsequences("abc", 3, "ac") → Print "ac".
Exclude 'c': printSubsequences("abc", 3, "a") → Print "a".
Exclude 'a': printSubsequences("abc", 1, "").
Include 'b': printSubsequences("abc", 2, "b").
Include 'c': printSubsequences("abc", 3, "bc") → Print "bc".
Exclude 'c': printSubsequences("abc", 3, "b") → Print "b".
Exclude 'b': printSubsequences("abc", 2, "").
Include 'c': printSubsequences("abc", 3, "c") → Print "c".
Exclude 'c': printSubsequences("abc", 3, "") (empty, so nothing is printed).
*/
To embed this project on your website, copy the following code and paste it into your website's HTML: