Skip to content

Commit ee3cacb

Browse files
committed
feat: recursion subsets added
1 parent 17d0ca3 commit ee3cacb

File tree

3 files changed

+56
-55
lines changed

3 files changed

+56
-55
lines changed

src/main/java/com/thealgorithms/LinkedList/LinkedListTraversal.java

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.Recursion;
2+
3+
// program to find power set of a string
4+
5+
import java.util.ArrayList;
6+
7+
public class Subsets {
8+
public static void main(String[] args) {
9+
String str = "abc";
10+
ArrayList<String> res = new ArrayList<>();
11+
subset("",str,res);
12+
System.out.println(res);
13+
}
14+
15+
public static void subset(String p,String up,ArrayList<String> res){
16+
if(up.isEmpty()){
17+
res.add(p);
18+
return;
19+
}
20+
21+
// Taking the character
22+
char ch = up.charAt(0);
23+
// Adding the character in the recursion
24+
subset(p+ch,up.substring(1),res);
25+
// Not adding the character in the recursion
26+
subset(p,up.substring(1),res);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.Recursion;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.ArrayList;
8+
9+
public class SubsetsTest {
10+
11+
@Test
12+
void subset(){
13+
String str = "abc";
14+
ArrayList<String> ans = new ArrayList<>();
15+
ArrayList<String> actual = new ArrayList<>();
16+
ans.add("abc");
17+
ans.add("ab");
18+
ans.add("ac");
19+
ans.add("a");
20+
ans.add("bc");
21+
ans.add("b");
22+
ans.add("c");
23+
ans.add("");
24+
25+
Subsets.subset("",str,actual);
26+
assertEquals(ans.toString(),actual.toString());
27+
}
28+
}

0 commit comments

Comments
 (0)