How to Generate All Subsequences of a String in Java or C++?

Question

How can I obtain all possible subsequence combinations of a string in Java or C++?

Answer

A subsequence of a string is a new string that is formed from the original string by deleting some characters without changing the order of the remaining characters. This article explains how to generate all subsequences of a given string in popular programming languages like Java and C++.

// Java implementation to generate all subsequences of a string
public class Subsequences {
    public static void main(String[] args) {
        String str = "abc";
        generateSubsequences(str, "");
    }

    public static void generateSubsequences(String str, String current) {
        if (str.length() == 0) {
            System.out.println(current);
            return;
        }
        // Exclude current character
        generateSubsequences(str.substring(1), current);
        // Include current character
        generateSubsequences(str.substring(1), current + str.charAt(0));
    }
} // Output: a, b, c, ab, ac, bc, abc, 

// C++ implementation to generate all subsequences of a string
#include <iostream>
#include <string>
using namespace std;

void generateSubsequences(string str, string current, int index) {
    if (index == str.length()) {
        cout << current << endl;
        return;
    }
    // Exclude current character
    generateSubsequences(str, current, index + 1);
    // Include current character
    generateSubsequences(str, current + str[index], index + 1);
}

int main() {
    string str = "abc";
    generateSubsequences(str, "", 0);
    return 0;
} // Output: a, b, c, ab, ac, bc, abc, 

Causes

  • Understanding the difference between subsequences and substrings.
  • Not accounting for empty subsequences which are valid.
  • Overlooking duplicate characters affecting the output.

Solutions

  • Use recursion to generate subsequences efficiently.
  • Utilize bit manipulation for a more concise solution.
  • Implement an iterative approach using loops.

Common Mistakes

Mistake: Not handling the empty subsequence case correctly.

Solution: Ensure to include logic in your function to print the current subsequence even if it's empty.

Mistake: Using complicated loops instead of recursion for this problem.

Solution: Utilize recursion or bit manipulation to simplify subsequence generation.

Helpers

  • generate subsequences
  • Java subsequence
  • C++ subsequence
  • string subsequence combinations
  • subsequence generation

Related Questions

⦿How to Enable Static Interface Methods Support Starting from Android N (API 24)?

Learn how to support static interface methods from Android N API 24 in your applications with clear explanations and code examples.

⦿How to Effectively Round Decimal Numbers in Android Programming

Learn how to round decimal numbers in Android with stepbystep guidance and code examples for precision in your applications.

⦿Understanding the Difference Between WEB-INF/lib Directory and Java 9 Modules

Explore the key differences between the WEBINFlib directory and Java 9 modules including usage structure and best practices.

⦿Understanding the Difference Between Local and Instance Variables in Java

Explore the key differences between local and instance variables in Java including their scopes lifetimes and use cases.

⦿How to Set the Alpha or Opacity of a Layout in Android?

Learn how to set alpha or opacity for layouts in Android and improve UI design with these expert tips.

⦿How to Retrieve Boolean Values from a Properties File in Java

Learn how to effectively retrieve boolean values from a properties file in Java with examples and common debugging tips.

⦿How to Calculate the Distance Between Two Points in Programming

Learn how to calculate the distance between two points in a 2D or 3D space using Python or JavaScript with clear code examples.

⦿How to Format a Double to Two Decimal Places in Programming?

Learn how to convert double values to display only two decimal places in programming languages like Java Python and JavaScript.

⦿How to Calculate the First and Last Day of Each Week in Java

Learn how to determine the first and last day of any week in Java with code examples and best practices.

⦿Why Is Declaring an Array as int[] a = new int[0] Valid in Java?

Explore why declaring an empty integer array in Java with int a new int0 is valid and how it works.

© Copyright 2025 - CodingTechRoom.com