Skip to content

Commit

Permalink
Power Set Using Recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
Raushan-Kumar007 committed Jun 22, 2022
1 parent 705b27d commit de2d01d
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions recursion/Problems/PowerSetUsingRecursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

/*You are given a string. You need to print the lexicographically sorted power-set of the string.
Note: The string s contains lowercase letter of alphabet.
Example 1:
Input:
s = a
Output: a
Explanation: empty string and a
are only sets.
Example 2:
Input:
s = abc
Output: a ab abc ac b bc c
Explanation: empty string,
a, ab, abc, ac, b, bc, c
are the sets.
Your Task:
You don't need to read input or print anything. You only need to complete the function powerSet that takes string s as parameter and returns a list of subsets. The lexicographic-sorting and printing is done automatically by the driver code.
Expected Time Complexity: O(2|s|).
Expected Auxiliary Space: O(|s|).
Constraints:
1 <= |s| <= 10 */




package recursion.Problems;

import java.util.ArrayList;

public class PowerSetUsingRecursion {
static ArrayList<String> powerSet(String s)
{
// add your code here
ArrayList<String> ls=new ArrayList<>();
getSet(s,"",0,s.length(),ls);
return ls;
}
public static void getSet(String s,String str,int i,int n,ArrayList<String> ls){
if(i==n){
ls.add(str);
return;
}
getSet(s,str,i+1,n,ls);
getSet(s,str+s.charAt(i),i+1,n,ls);
}
}

0 comments on commit de2d01d

Please sign in to comment.