原文:

给定一个字符串,我们必须找出它的所有子序列。字符串是给定字符串的子序列,它是通过删除给定字符串的某些字符而不改变其顺序来生成的。

示例:

input : abc
output : a, b, c, ab, bc, ac, abc
input : aaa
output : a, aa, aaa

方法 1(挑与不挑概念)

c

// c   program for the above approach
#include 
using namespace std;
// find all subsequences
void printsubsequence(string input, string output)
{
    // base case
    // if the input is empty print the output string
    if (input.empty()) {
        cout << output << endl;
        return;
    }
    // output is passed with including
    // the ist character of
    // input string
    printsubsequence(input.substr(1), output   input[0]);
    // output is passed without
    // including the ist character
    // of input string
    printsubsequence(input.substr(1), output);
}
// driver code
int main()
{
    // output is set to null before passing in as a
    // parameter
    string output = "";
    string input = "abcd";
    printsubsequence(input, output);
    return 0;
}

java

// java program for the above approach
import java.util.*;
class gfg {
    // declare a global list
    static list al = new arraylist<>();
    // creating a public static arraylist such that
    // we can store values
    // if there is any question of returning the
    // we can directly return too// public static
    // arraylist al = new arraylist();
    public static void main(string[] args)
    {
        string s = "abcd";
        findsubsequences(s, ""); // calling a function
        system.out.println(al);
    }
    private static void findsubsequences(string s,
                                         string ans)
    {
        if (s.length() == 0) {
            al.add(ans);
            return;
        }
        // we add adding 1st character in string
        findsubsequences(s.substring(1), ans   s.charat(0));
        // not adding first character of the string
        // because the concept of subsequence either
        // character will present or not
        findsubsequences(s.substring(1), ans);
    }
}

python 3

# below is the implementation of the above approach
def printsubsequence(input, output):
    # base case
    # if the input is empty print the output string
    if len(input) == 0:
        print(output, end=' ')
        return
    # output is passed with including the
    # 1st character of input string
    printsubsequence(input[1:], output input[0])
    # output is passed without including the
    # 1st character of input string
    printsubsequence(input[1:], output)
# driver code
# output is set to null before passing in
# as a parameter
output = ""
input = "abcd"
printsubsequence(input, output)
# this code is contributed by tharun reddy

c

// c# program for the above approach
using system;
using system.collections.generic;
class gfg{
static void printsubsequence(string input,
                             string output)
{
    // base case
    // if the input is empty print the output string
    if (input.length == 0)
    {
        console.writeline(output);
        return;
    }
    // output is passed with including
    // the ist character of
    // input string
    printsubsequence(input.substring(1),
                     output   input[0]);
    // output is passed without
    // including the ist character
    // of input string
    printsubsequence(input.substring(1),
                     output);
}
// driver code
static void main()
{
    // output is set to null before passing
    // in as a parameter
    string output = "";
    string input = "abcd";
    printsubsequence(input, output);
}
}
// this code is contributed by soumikmondal

javascript


输出

abcd
abc
abd
ab
acd
ac
ad
a
bcd
bc
bd
b
cd
c
d