
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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