VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-subsequences-string-iterative-method/

⇱ Print all subsequences of a string | Iterative Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print all subsequences of a string | Iterative Method

Last Updated : 20 Feb, 2023

Given a string s, print all possible subsequences of the given string in an iterative manner. We have already discussed Recursive method to print all subsequences of a string

Examples:  

Input : abc
Output : a, b, c, ab, ac, bc, abc

Input : aab
Output : a, b, aa, ab, aab

Approach 1 : 
Here, we discuss much easier and simpler iterative approach which is similar to Power Set. We use bit pattern from binary representation of 1 to 2^length(s) - 1.

input = "abc" 
Binary representation to consider 1 to (2^3-1), i.e 1 to 7. 
Start from left (MSB) to right (LSB) of binary representation and append characters from input string which corresponds to bit value 1 in binary representation to Final subsequence string sub.

Example: 

001 => abc . Only c corresponds to bit 1. So, subsequence = c. 
101 => abc . a and c corresponds to bit 1. So, subsequence = ac.
binary_representation (1) = 001 => c 
binary_representation (2) = 010 => b 
binary_representation (3) = 011 => bc 
binary_representation (4) = 100 => a 
binary_representation (5) = 101 => ac 
binary_representation (6) = 110 => ab 
binary_representation (7) = 111 => abc

Below is the implementation of above approach: 


Output
Subsequences of length = 1 are:
a b c 
Subsequences of length = 2 are:
aa ab ac bc 
Subsequences of length = 3 are:
aab aac abc 
Subsequences of length = 4 are:
aabc 

Time Complexity : O(2^n), where n is length of string to find subsequences and n is length of binary string.
Space Complexity: O(1)

Approach 2 : 

Approach is to get the position of rightmost set bit and reset that bit after appending corresponding character from given string to the subsequence and will repeat the same thing till corresponding binary pattern has no set bits.

If input is s = “abc” 
Binary representation to consider 1 to (2^3-1), i.e 1 to 7. 
001 => abc . Only c corresponds to bit 1. So, subsequence = c 
101 => abc . a and c corresponds to bit 1. So, subsequence = ac.
Let us use Binary representation of 5, i.e 101. 
Rightmost bit is at position 1, append character at beginning of sub = c ,reset position 1 => 100 
Rightmost bit is at position 3, append character at beginning of sub = ac ,reset position 3 => 000 
As now we have no set bit left, we stop computing subsequence.

Example:

binary_representation (1) = 001 => c 
binary_representation (2) = 010 => b 
binary_representation (3) = 011 => bc 
binary_representation (4) = 100 => a 
binary_representation (5) = 101 => ac 
binary_representation (6) = 110 => ab 
binary_representation (7) = 111 => abc

Below is the implementation of above approach : 


Output
Subsequences of length = 1 are:
a b c 
Subsequences of length = 2 are:
aa ab ac bc 
Subsequences of length = 3 are:
aab aac abc 
Subsequences of length = 4 are:
aabc 

Time Complexity: , where n is the length of string to find subsequence and b is the number of set bits in binary string.
Auxiliary Space: O(n)

Comment
Article Tags:
Article Tags: