-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion9.java
More file actions
30 lines (23 loc) · 783 Bytes
/
Recursion9.java
File metadata and controls
30 lines (23 loc) · 783 Bytes
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
import java.util.HashSet;
public class Recursion9 {
public static void subsequences(String str, int i, String newStr, HashSet<String> set) {
// Base Case
if (i == str.length()) {
if (!set.contains(newStr)) {
set.add(newStr);
System.out.println(newStr);
}
return;
}
char currChar = str.charAt(i);
// 1️⃣ Include current character
subsequences(str, i + 1, newStr + currChar, set);
// 2️⃣ Exclude current character
subsequences(str, i + 1, newStr, set);
}
public static void main(String[] args) {
String str = "aaa";
HashSet<String> set = new HashSet<>();
subsequences(str, 0, "", set);
}
}