Generate all possible Subsets of given string

PHOTO EMBED

Sun Feb 06 2022 21:45:23 GMT+0000 (Coordinated Universal Time)

Saved by @Uttam #java #gfg #geeksforgeeks #lecture #recursion #generate subsets

import java.io.*;
import java.util.*;

class GFG 
{
	static void printSub(String str, String curr, int index)
	{
		if(index == str.length())
		{
			System.out.print(curr+" ");
			return;
		}

		printSub(str, curr, index + 1);
		printSub(str, curr+str.charAt(index), index + 1);
	}
  
    public static void main(String [] args) 
    {
    	String str = "ABC";
    	
    	printSub(str, "", 0); 
    }
}
content_copyCOPY

NOTE : All Characters in the input string are distinct. For a string of length n, there are going to be 2^n subsets. Examples: Input : set = "ab" Output : "", "a", "b", "ab" Input : set = "abc" Output : "", "a", "b", "c", "ab", "ac", "bc", "abc" Input : set = "abcd" Output : "" "a" "ab" "abc" "abcd" "abd" "ac" "acd" "ad" "b" "bc" "bcd" "bd" "c" "cd" "d"