Skip to content

Commit 41f4818

Browse files
committed
prints all subsequences of given string, recursively
1 parent e6cf6d2 commit 41f4818

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package Lecture11;
2+
3+
public class printStringSubsequences {
4+
5+
public static void main(String[] args) {
6+
7+
String str = "abc";
8+
String result = "";
9+
10+
printSubsequences(str, result);
11+
}
12+
13+
public static void printSubsequences(String str, String result) {
14+
15+
if (str.length() == 0) {
16+
System.out.println(result);
17+
return;
18+
}
19+
20+
char first = str.charAt(0);
21+
String restStr = str.substring(1);
22+
23+
printSubsequences(restStr, result);
24+
25+
printSubsequences(restStr, result + first);
26+
}
27+
}
28+
29+
/*
30+
output:
31+
32+
c
33+
b
34+
bc
35+
a
36+
ac
37+
ab
38+
abc
39+
*/
40+
// note first line of output here is blank space which is also the part of subsequence

0 commit comments

Comments
 (0)