Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b3d3670

Browse files
Add implementation for generating all subsequences of a string
1 parent f961e8d commit b3d3670

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎Recursion/Subsequence.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
import java.util.HashSet;
3+
4+
public class Subsequence {
5+
public static void main(String[] args) {
6+
HashSet<String> set = new HashSet<>();
7+
String str = "aaa";
8+
SubsequenceGenerator(0, str, "",set);
9+
}
10+
11+
static void SubsequenceGenerator(int index , String str,String newStr, HashSet<String> set){
12+
if(index == str.length()){
13+
if(set.contains(newStr)){
14+
return;
15+
}else{
16+
set.add(newStr);
17+
System.out.println(newStr);
18+
return;
19+
}
20+
}
21+
char currChar =str.charAt(index);
22+
23+
SubsequenceGenerator(index +1, str, newStr+currChar,set);
24+
25+
SubsequenceGenerator(index+1, str, newStr,set);
26+
27+
}
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /