forked from ya7sh/oct2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PR1.java
41 lines (33 loc) · 954 Bytes
/
PR1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/****************************************************************************
* Code to print all subsets of a string with recursion
*******************************************************************************/
import java.util.*;
public class Main
{
public static void main(String[] args) {
subseq("","abc");
}
static void subseq(String p,String up)
{
if(up.isEmpty()){
System.out.println(p);
return;
}
char ch = up.charAt(0);
subseq(p+ch,up.substring(1));
subseq(p,up.substring(1));
}
static ArrayList<String> Subseq(String p,String up)
{
if(up.isEmpty()){
ArrayList<String> list = new ArrayList<>();
list.add(p);
return list;
}
char ch = up.charAt(0);
ArrayList<String> left = Subseq(p+ch,up.substring(1));
ArrayList<String> right = Subseq(p,up.substring(1));
left.addAll(right);
return left;
}
}