Header Ads Widget

Responsive Advertisement

Print All Subsequences for a String...in JAVA


 


 import java.util.*;

public class Recursion2 {

    public static void subsequences(String str, int idx, String newString) {
        if (idx == str.length()) {
            System.out.println(newString);
            return;
        }

        char currentChar = str.charAt(idx);

        // To be ...
        subsequences(str, idx + 1, newString + currentChar);

        // Not to be...
        subsequences(str, idx + 1, newString);
    }
public static void main(String[] args) {

        String str = "aabbcc";
       
       
        subsequences(str, 0, "");
       

    }
}

OUTPUT :

abc ab ac a bc b c

Post a Comment

0 Comments