Given a string str, the task is to print all the sub-sequences of str.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Examples:
Input: str = “abc”
Output: a b ab c ac bc abc
Input: str = “geek”
Output: g e ge e ge ee gee k gk ek gek ek gek eek geek
Approach: Write a recursive function that prints every sub-sequence of the sub-string starting from the second character str[1, n – 1] after appending the first character of the string str[0] in the beginning of every sub-sequence. Terminating condition will be when the passed string is empty, in that case the function will return an empty arraylist.
Below is the implementation of the above approach:
Java
// Java implementation of the approachimport java.util.ArrayList;public class GFG { // Utility function to print the contents // of the ArrayList static void printArrayList(ArrayList<String> 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<String> getSequence(String str) { // If string is empty if (str.length() == 0) { // Return an empty arraylist ArrayList<String> 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<String> 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<String> 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)); }} |
C#
// C# implementation of the approachusing System;using System.Collections.Generic;class GFG{ // Utility function to print the contents // of the List static void printList(List<String> 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<String> getSequence(String str) { // If string is empty if (str.Length == 0) { // Return an empty arraylist List<String> empty = new List<String>(); 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<String> 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<String> res = new List<String>(); 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 |
g e ge e ge ee gee k gk ek gek ek gek eek geek
Alternate Solution: One by one fix characters and recursively generate all subsets starting from them.
Java
// Java implementation of the approachpublic 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, ""); }} |
C#
// C# implementation of the approachusing 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
Recommended Posts:
- ArrayList of ArrayList in Java
- Print all permutation of a string using ArrayList
- Java ArrayList to print all possible words from phone digits
- Print all subsequences of a string | Iterative Method
- Print all subsequences of a string in Python
- Print all subsequences of a string
- Convert an ArrayList of String to a String array in Java
- Print all possible K-length subsequences of first N natural numbers with sum N
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Remove all elements from the ArrayList in Java
- How to add all items from a collection to an ArrayList in Java?
- Minimize deletions in a Binary String to remove all subsequences of the form "0101"
- Count subsequences in first string which are anagrams of the second string
- Construct a string that has exactly K subsequences from given string
- Check if string S2 can be obtained by appending subsequences of string S1
- Generate all distinct subsequences of array using backtracking
- Generating all possible Subsequences using Recursion
- Graph Representation using Java ArrayList
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Count all increasing subsequences
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


