-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathReturnSubsequences.java
More file actions
35 lines (30 loc) · 1001 Bytes
/
ReturnSubsequences.java
File metadata and controls
35 lines (30 loc) · 1001 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
31
32
33
34
35
package recursion3;
import java.util.Scanner;
public class ReturnSubsequences {
public static String[] findSubsequences(String str) {
if (str.length() == 0) {
return new String[]{" "};
}
String[] smallAnswer = findSubsequences(str.substring(1));
String[] answer = new String[2 * smallAnswer.length];
for (int i = 0; i < smallAnswer.length; i++) {
answer[i] = smallAnswer[i];
}
for (int i = 0; i < smallAnswer.length; i++) {
answer[i + smallAnswer.length] = str.charAt(0) + smallAnswer[i];
}
return answer;
}
public static void printArray(String[] arr) {
for (String s : arr) {
System.out.print(s + "\t");
}
System.out.println();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
String[] arr = findSubsequences(str);
printArray(arr);
}
}