-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathAllSubsetsUsingRecursion.java
84 lines (79 loc) · 1.5 KB
/
AllSubsetsUsingRecursion.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.java.strings;
import java.util.Scanner;
/*
* Find All Subsets using Recursion approach
* -----------------------------------------
*
* The subset of a string is the character
* or the group of characters that are present
* inside the string.
* All the possible subsets for a string
* will be 2^n (n is the no of characters).
*
* say Given String is "abc"
* { }
* { a }
* { a b }
* { a b c }
* { a c }
* { b }
* { b c }
* { c }
*
*/
public class AllSubsetsUsingRecursion {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter any String : ");
String str = scanner.nextLine().trim();
printAllSubsets(str);
scanner.close();
}
//using recursion approach
public static void printAllSubsets(String str) {
char array[] =str.toCharArray();
int visited[] = new int[array.length];
traverse(array, visited,0);
}
public static void traverse(char array[],int visited[],int curIndex){
System.out.print("\n{ ");
for(int i=0;i<visited.length;i++)
if(visited[i] == 1)
System.out.print(array[i]+" ");
System.out.print("}");
for(int j=curIndex;j<array.length;j++){
visited[j] = 1;
traverse(array, visited,j+1);
visited[j] = 0;
}
}
}
/*
OUTPUT
Enter any String : abc
{ }
{ a }
{ a b }
{ a b c }
{ a c }
{ b }
{ b c }
{ c }
Enter any String : four
{ }
{ f }
{ f o }
{ f o u }
{ f o u r }
{ f o r }
{ f u }
{ f u r }
{ f r }
{ o }
{ o u }
{ o u r }
{ o r }
{ u }
{ u r }
{ r }
*/