📌  相关文章
📜  使用ArrayList打印字符串的所有子序列

📅  最后修改于: 2021-04-29 07:51:11             🧑  作者: Mango

给定一个字符串str,任务是打印海峡的所有子序列。
子序列是一个序列,可以通过删除一些元素或不删除某些元素而无需更改其余元素的顺序来从另一个序列中得出。
例子:

方法:编写一个递归函数,在每个子序列的开头附加字符串str [0]的第一个字符后从第二个字符str [1,n – 1]开始打印子字符串的每个子序列。终止条件为传递的字符串为空时,在这种情况下,该函数将返回一个空的arraylist。
下面是上述方法的实现:

Java
// Java implementation of the approach
import java.util.ArrayList;
 
public class GFG {
 
    // Utility function to print the contents
    // of the ArrayList
    static void printArrayList(ArrayList arrL)
    {
        arrL.remove("");
        for (int i = 0; i < arrL.size(); i++)
            System.out.print(arrL.get(i) + " ");
    }
 
    // Function to returns the arraylist which contains
    // all the sub-sequences of str
    public static ArrayList getSequence(String str)
    {
 
        // If string is empty
        if (str.length() == 0) {
 
            // Return an empty arraylist
            ArrayList empty = new ArrayList<>();
            empty.add("");
            return empty;
        }
 
        // Take first character of str
        char ch = str.charAt(0);
 
        // Take sub-string starting from the
        // second character
        String subStr = str.substring(1);
 
        // Recurvise call for all the sub-sequences
        // starting from the second character
        ArrayList subSequences =
                              getSequence(subStr);
 
        // Add first character from str in the beginning
        // of every character from the sub-sequences
        // and then store it into the resultant arraylist
        ArrayList res = new ArrayList<>();
        for (String val : subSequences) {
            res.add(val);
            res.add(ch + val);
        }
 
        // Return the resultant arraylist
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "geek";
        printArrayList(getSequence(str));
        // System.out.print(getSequence(str));
    }
}


Python3
# Python implementation of the approach
 
# Utility function to print the contents
# of the ArrayList
def printArrayList(arrL):
    arrL.remove("")
    print(*arrL, sep = " ")
 
# Function to returns the arraylist which contains
# all the sub-sequences of str
def getSequence(Str):
 
    # If string is empty
    if(len(Str) == 0):
 
        # Return an empty arraylist
        empty = []
        empty.append("")
        return empty
 
    # Take first character of str
    ch = Str[0]
 
    # Take sub-string starting from the
    # second character
    subStr = Str[1:]
 
    # Recurvise call for all the sub-sequences
    # starting from the second character
    subSequences = getSequence(subStr)
 
    # Add first character from str in the beginning
    # of every character from the sub-sequences
    # and then store it into the resultant arraylist
    res = []
 
    for val in subSequences:
        res.append(val)
        res.append(ch + val)
 
    # Return the resultant arraylist
    return res
 
# Driver code
Str = "geek"
printArrayList(getSequence(Str))
 
# This code is contributed by avanitrachhadiya2155


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG{
 
    // Utility function to print the contents
    // of the List
    static void printList(List arrL)
    {
        arrL.Remove("");
        for (int i = 0; i < arrL.Count; i++)
            Console.Write(arrL[i] + " ");
    }
 
    // Function to returns the arraylist which contains
    // all the sub-sequences of str
    public static List getSequence(String str)
    {
 
        // If string is empty
        if (str.Length == 0)
        {
 
            // Return an empty arraylist
            List empty = new List();
            empty.Add("");
            return empty;
        }
 
        // Take first character of str
        char ch = str[0];
 
        // Take sub-string starting from the
        // second character
        String subStr = str.Substring(1);
 
        // Recurvise call for all the sub-sequences
        // starting from the second character
        List subSequences = getSequence(subStr);
 
        // Add first character from str in the beginning
        // of every character from the sub-sequences
        // and then store it into the resultant arraylist
        List res = new List();
        foreach (String val in subSequences)
        {
            res.Add(val);
            res.Add(ch + val);
        }
 
        // Return the resultant arraylist
        return res;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String str = "geek";
        printList(getSequence(str));
        // Console.Write(getSequence(str));
    }
}
 
// This code is contributed by Rohit_ranjan


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print all the sub-sequences
// of a string
void printSubSeq(string sub, string ans)
{
 
    if (sub.length() == 0) {
        cout << "" << ans << " ";
        return;
    }
 
    // First character of sub
    char ch = sub[0];
 
    // Sub-string starting from second
    // character of sub
    string ros = sub.substr(1);
 
    // Excluding first character
    printSubSeq(ros, ans);
 
    // Including first character
    printSubSeq(ros, ans + ch);
}
 
// Driver code
int main()
{
    string str = "abc";
    printSubSeq(str, "");
 
    return 0;
}
 
// This code is contributed by divyesh072019


Java
// Java implementation of the approach
public class sub_sequence {
 
    // Function to print all the sub-sequences
    // of a string
    public static void printSubSeq(String sub,
                                  String ans)
    {
 
        if (sub.length() == 0) {
            System.out.print("" + ans + " ");
            return;
        }
 
        // First character of sub
        char ch = sub.charAt(0);
 
        // Sub-string starting from second
        // character of sub
        String ros = sub.substring(1);
 
        // Excluding first character
        printSubSeq(ros, ans);
 
        // Including first character
        printSubSeq(ros, ans + ch);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "abc";
        printSubSeq(str, "");
    }
}


Python3
# Python3 implementation of the approach
 
# Function to print all the sub-sequences
# of a string
def printSubSeq(sub, ans) :
 
    if (len(sub) == 0) :
        print(ans , end = " ")
        return
 
    # First character of sub
    ch = sub[0]
 
    # Sub-string starting from second
    # character of sub
    ros = sub[1 : ]
 
    # Excluding first character
    printSubSeq(ros, ans)
 
    # Including first character
    printSubSeq(ros, ans + ch)
 
Str = "abc"
printSubSeq(Str, "")
 
# This code iscontributed by divyeshrabadiya07


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to print all the 
// sub-sequences of a string
public static void printSubSeq(string sub,
                               string ans)
{
    if (sub.Length == 0)
    {
        Console.Write("" + ans + " ");
        return;
    }
 
    // First character of sub
    char ch = sub[0];
 
    // Sub-string starting from second
    // character of sub
    string ros = sub.Substring(1);
 
    // Excluding first character
    printSubSeq(ros, ans);
 
    // Including first character
    printSubSeq(ros, ans + ch);
}
 
// Driver code
public static void Main()
{
    string str = "abc";
    printSubSeq(str, "") ;
}
}
 
// This code is contributed by Ryuga


输出:
g e ge e ge ee gee k gk ek gek ek gek eek geek

替代解决方案:一对一修复字符,并从它们开始递归地生成所有子集。

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print all the sub-sequences
// of a string
void printSubSeq(string sub, string ans)
{
 
    if (sub.length() == 0) {
        cout << "" << ans << " ";
        return;
    }
 
    // First character of sub
    char ch = sub[0];
 
    // Sub-string starting from second
    // character of sub
    string ros = sub.substr(1);
 
    // Excluding first character
    printSubSeq(ros, ans);
 
    // Including first character
    printSubSeq(ros, ans + ch);
}
 
// Driver code
int main()
{
    string str = "abc";
    printSubSeq(str, "");
 
    return 0;
}
 
// This code is contributed by divyesh072019

Java

// Java implementation of the approach
public class sub_sequence {
 
    // Function to print all the sub-sequences
    // of a string
    public static void printSubSeq(String sub,
                                  String ans)
    {
 
        if (sub.length() == 0) {
            System.out.print("" + ans + " ");
            return;
        }
 
        // First character of sub
        char ch = sub.charAt(0);
 
        // Sub-string starting from second
        // character of sub
        String ros = sub.substring(1);
 
        // Excluding first character
        printSubSeq(ros, ans);
 
        // Including first character
        printSubSeq(ros, ans + ch);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "abc";
        printSubSeq(str, "");
    }
}

Python3

# Python3 implementation of the approach
 
# Function to print all the sub-sequences
# of a string
def printSubSeq(sub, ans) :
 
    if (len(sub) == 0) :
        print(ans , end = " ")
        return
 
    # First character of sub
    ch = sub[0]
 
    # Sub-string starting from second
    # character of sub
    ros = sub[1 : ]
 
    # Excluding first character
    printSubSeq(ros, ans)
 
    # Including first character
    printSubSeq(ros, ans + ch)
 
Str = "abc"
printSubSeq(Str, "")
 
# This code iscontributed by divyeshrabadiya07

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to print all the 
// sub-sequences of a string
public static void printSubSeq(string sub,
                               string ans)
{
    if (sub.Length == 0)
    {
        Console.Write("" + ans + " ");
        return;
    }
 
    // First character of sub
    char ch = sub[0];
 
    // Sub-string starting from second
    // character of sub
    string ros = sub.Substring(1);
 
    // Excluding first character
    printSubSeq(ros, ans);
 
    // Including first character
    printSubSeq(ros, ans + ch);
}
 
// Driver code
public static void Main()
{
    string str = "abc";
    printSubSeq(str, "") ;
}
}
 
// This code is contributed by Ryuga
输出:
c b bc a ac ab abc