# Write a program to print all subset of a given string recursively

In this article we will learn how to write a program to print all subset of a given string recursively.

Let us first understand it with example. Suppose given string be "NET" then total number of subset which can be obtained using the string will be-

• 2(number of characters) - 1 i.e. = 23 - 1 = 7 = {NET,NE,NT,N,ET,E,T}
• 2(number of characters) if we include empty subset. =  23 = 8 = {NET,NE,NT,N,ET,E,T, ϕ }

## Approach

At each step, we isolate an element from the remainder and then recursively list those sets that include that element, and recur again to build those sets that don't contain that element. In each case, the set of remaining elements is one smaller and thus represents a slightly easier, smaller version of the same problem. Those recursive calls will eventually lead to the simplest case, that of listing the subsets of an empty set. Here is the pseudocode description:

If there are no more elements remaining,
print current subset
else {
Consider the next element of those remaining
Try adding it to the current subset, and use recursion to build subsets from here
Try not adding it to current subset, and use recursion to build subsets from here
}


## Implementation

/**
* @author Amit Gupta
*
*/
public class SubsetPattern {

public static void main(String[] args) {

recSubsets("", "NET");
}

/** This method prints all subset of given string
* @param soFar
* @param rest
*/
public static void recSubsets(String soFar, String rest) {
if (rest.length() == 0) {
System.out.println(soFar);
} else {
recSubsets(soFar + rest.charAt(0), rest.substring(1)); // include first  char

recSubsets(soFar, rest.substring(1)); // exclude first char
}
}
}


Output

NET
NE
NT
N
ET
E
T


In this article we learnt how to write a program to print all subset of a given string recursively.

Article tagged as
Author
Author: Amit Gupta
Published On: 14/10/2016
Last revised On: 10/15/2016 1:30:41 PM
View all articles by Amit Gupta
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.30731332302093506, "perplexity": 2465.880008322112}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2017-22/segments/1495463612069.19/warc/CC-MAIN-20170529091944-20170529111944-00088.warc.gz"}