Print all subsequences of a string using ArrayList
Last Updated :
01 Nov, 2023
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:
C++
#include <bits/stdc++.h>
using namespace std;
void printList(vector<string> arrL)
{
arrL.erase(find(arrL.begin(), arrL.end(), "" ));
for ( int i = 0; i < arrL.size(); i++)
cout << arrL[i] << " " ;
}
vector<string> getSequence(string str)
{
if (str.length() == 0)
{
vector<string> empty;
empty.push_back( "" );
return empty;
}
char ch = str[0];
string subStr = str.substr(1);
vector<string> subSequences = getSequence(subStr);
vector<string> res;
for (string val : subSequences)
{
res.push_back(val);
res.push_back(ch + val);
}
return res;
}
int main()
{
string str = "geek" ;
printList(getSequence(str));
return 0;
}
|
Java
import java.util.ArrayList;
public class GFG {
static void printArrayList(ArrayList<String> arrL)
{
arrL.remove( "" );
for ( int i = 0 ; i < arrL.size(); i++)
System.out.print(arrL.get(i) + " " );
}
public static ArrayList<String> getSequence(String str)
{
if (str.length() == 0 ) {
ArrayList<String> empty = new ArrayList<>();
empty.add( "" );
return empty;
}
char ch = str.charAt( 0 );
String subStr = str.substring( 1 );
ArrayList<String> subSequences =
getSequence(subStr);
ArrayList<String> res = new ArrayList<>();
for (String val : subSequences) {
res.add(val);
res.add(ch + val);
}
return res;
}
public static void main(String[] args)
{
String str = "geek" ;
printArrayList(getSequence(str));
}
}
|
Python3
def printArrayList(arrL):
arrL.remove("")
print ( * arrL, sep = " " )
def getSequence( Str ):
if ( len ( Str ) = = 0 ):
empty = []
empty.append("")
return empty
ch = Str [ 0 ]
subStr = Str [ 1 :]
subSequences = getSequence(subStr)
res = []
for val in subSequences:
res.append(val)
res.append(ch + val)
return res
Str = "geek"
printArrayList(getSequence( Str ))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void printList(List<String> arrL)
{
arrL.Remove( "" );
for ( int i = 0; i < arrL.Count; i++)
Console.Write(arrL[i] + " " );
}
public static List<String> getSequence(String str)
{
if (str.Length == 0)
{
List<String> empty = new List<String>();
empty.Add( "" );
return empty;
}
char ch = str[0];
String subStr = str.Substring(1);
List<String> subSequences = getSequence(subStr);
List<String> res = new List<String>();
foreach (String val in subSequences)
{
res.Add(val);
res.Add(ch + val);
}
return res;
}
public static void Main(String[] args)
{
String str = "geek" ;
printList(getSequence(str));
}
}
|
Javascript
<script>
function printArrayList(arrL)
{
arrL.splice(arrL.indexOf( "" ), 1);
for (let i = 0; i < arrL.length; i++)
document.write(arrL[i] + " " );
}
function getSequence(str)
{
if (str.length == 0) {
let empty = [];
empty.push( "" );
return empty;
}
let ch = str[0];
let subStr = str.substring(1);
let subSequences = getSequence(subStr);
let res = [];
for (let val = 0; val < subSequences.length; val++) {
res.push(subSequences[val]);
res.push(ch + subSequences[val]);
}
return res;
}
let str = "geek" ;
printArrayList(getSequence(str));
</script>
|
Output
g e ge e ge ee gee k gk ek gek ek gek eek geek
Time Complexity: O(2^n), where n is the length of the string.
Space Complexity: O(2^n), as the recursive stack grows until the length of the string.
Alternate Solution: One by one fix characters and recursively generate all subsets starting from them.
C++
#include <bits/stdc++.h>
using namespace std;
void printSubSeq(string sub, string ans)
{
if (sub.length() == 0) {
cout << "" << ans << " " ;
return ;
}
char ch = sub[0];
string ros = sub.substr(1);
printSubSeq(ros, ans);
printSubSeq(ros, ans + ch);
}
int main()
{
string str = "abc" ;
printSubSeq(str, "" );
return 0;
}
|
Java
public class sub_sequence {
public static void printSubSeq(String sub,
String ans)
{
if (sub.length() == 0 ) {
System.out.print( "" + ans + " " );
return ;
}
char ch = sub.charAt( 0 );
String ros = sub.substring( 1 );
printSubSeq(ros, ans);
printSubSeq(ros, ans + ch);
}
public static void main(String[] args)
{
String str = "abc" ;
printSubSeq(str, "" );
}
}
|
Python3
def printSubSeq(sub, ans) :
if ( len (sub) = = 0 ) :
print (ans , end = " " )
return
ch = sub[ 0 ]
ros = sub[ 1 : ]
printSubSeq(ros, ans)
printSubSeq(ros, ans + ch)
Str = "abc"
printSubSeq( Str , "")
|
C#
using System;
class GFG
{
public static void printSubSeq( string sub,
string ans)
{
if (sub.Length == 0)
{
Console.Write( "" + ans + " " );
return ;
}
char ch = sub[0];
string ros = sub.Substring(1);
printSubSeq(ros, ans);
printSubSeq(ros, ans + ch);
}
public static void Main()
{
string str = "abc" ;
printSubSeq(str, "" ) ;
}
}
|
Javascript
<script>
function printSubSeq(sub, ans)
{
if (sub.length == 0)
{
document.write( "" + ans + " " );
return ;
}
let ch = sub[0];
let ros = sub.substring(1);
printSubSeq(ros, ans);
printSubSeq(ros, ans + ch);
}
let str = "abc" ;
printSubSeq(str, "" ) ;
</script>
|
Output
c b bc a ac ab abc
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...