
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all subsequences of a string in C++
In this problem, we are given a string and we have to print all the subsequences of the string. The substring generated is created by deleting the elements of the string but the order remains the same(i.e. Order cannot be changed).
Let’s take an example to understand the topic better −
Input: xyz Output: x,y,z,xy,yz,xz,xyz
Explanation − In the above example, we can see the only characters are deleted to create substring. No, rearranging takes place.
There can be multiple methods to solve this problem, here we will discuss a few of them to understand methods.
One is by selecting elements of the string and eliminating a few to create a sequence. In this method, we will pick a few elements and delete the rest to create the substring.
Example
import java.util.*; class Main{ public static ArrayList<String>subStringSeq=new ArrayList<String>(); public static void main(String[] args) { String s="pqrs"; System.out.println("All the substring found are :"); findSubString(s,""); System.out.println(subStringSeq); } public static void findSubString(String s, String ans) { if(s.length()==0){ subStringSeq.add(ans); return; } findSubString(s.substring(1),ans+s.charAt(0)) ; findSubString(s.substring(1),ans); } }
Output
All the substring found are −
[pqrs, pqr, pqs, pq, prs, pr, ps, p, qrs, qr, qs, q, rs, r, s, ]
Another method could be iterating over the string and generate substring. And dropping characters of the sequence to generate substrings. Here, we will use a list to store the substrings. And check if the sequence found is already found or not.
Example
import java.util.HashSet; public class Main{ static HashSet<String> subString = new HashSet<>(); static void findSubString(String str){ for (int i = 0; i < str.length(); i++) { for (int j = str.length(); j > i; j--) { String sub_str = str.substring(i, j); if (!subString.contains(sub_str)) subString.add(sub_str); for (int k = 1; k < sub_str.length() - 1; k++) { StringBuffer sb = new StringBuffer(sub_str); sb.deleteCharAt(k); if (!subString.contains(sb)); findSubString(sb.toString()); } } } } public static void main(String[] args){ String s = "pqrs"; System.out.println("The subsequence is "); findSubString(s); System.out.println(subString); } }
Output
The subsequence is
[rs, pq, qr, pr, qs, ps, prs, p, pqr, q, r, s, pqs, qrs, pqrs]
One more method can be fix characters and find substring. In this method we will fix elements of the string one by one and using these fixed characters, we will find the subsequence. Recursive calling of this method creates the required string subsequence.
Example
class Main { static void subString(String str, int n, int index, String curr){ if (index == n){ return; } System.out.print(curr + ", "); for (int i = index + 1; i < n; i++){ curr += str.charAt(i); subString(str, n, i, curr); curr = curr.substring(0, curr.length() - 1); } } static void printSubStrings(String str){ int index = -1; String curr = ""; subString(str, str.length(), index, curr); } public static void main(String[] args){ String str = "pqrs"; System.out.println("The subStrings are :") ; printSubStrings(str); } }
Output
The subStrings are −
p, pq, pqr, pqrs, pqs, pr, prs, ps, q, qr, qrs, qs, r, rs, s
- Related Articles
- Print all subsequences of a string using ArrayList in C++
- Print all subsequences of a string using Iterative Method in C++
- Print all Subsequences of String which Start with Vowel and End with Consonant in C++
- Print all permutations of a string in Java
- Print all permutations of a given string
- Print all palindromic partitions of a string in C++
- Print all palindrome permutations of a string in C++
- Print all permutation of a string using ArrayList in Java
- Print all distinct characters of a string in order in C++
- Print all funny words in a string in C++
- Program to print all substrings of a given string in C++
- Count all increasing subsequences in C++
- Print all the combinations of a string in lexicographical order in C++
- Java Program to Print all unique words of a String
- Python Program to print all permutations of a given string
