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

Appearance settings

Commit 5fe7aa5

Browse filesBrowse files
Recursion Day 10 is Completed
1 parent d859d71 commit 5fe7aa5
Copy full SHA for 5fe7aa5

File tree

3 files changed

+81
-0
lines changed
Filter options

3 files changed

+81
-0
lines changed

‎ArrayBasedProblems/SubSequenceInArray.java

Copy file name to clipboardExpand all lines: ArrayBasedProblems/SubSequenceInArray.java
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ArrayBasedProblems;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45

56
public class SubSequenceInArray {
67

@@ -39,6 +40,7 @@ private static void listSubSequence() { // time complexity - o(n*2^n) duplicatio
3940

4041
private static void listSubSequenceDuplicate() { // time complexity - o(n*2^n) with duplication allow
4142
int nums[] = { 1, 2, 2 };
43+
Arrays.sort(nums);
4244
ArrayList<ArrayList<Integer>> outer = new ArrayList<>();
4345
outer.add(new ArrayList<>());
4446
int start = 0, end = 0;

‎Recursion/LetterCombination.java

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package Recursion;
2+
3+
public class LetterCombination {
4+
5+
private static void pad(String str,String ans) {
6+
if(str.length()==0) {
7+
System.out.println(ans);
8+
return;
9+
}
10+
11+
int digit = str.charAt(0) - '0';
12+
for(int i = (digit-1)*3;i<(digit*3);i++) {
13+
char ch = (char) ('a'+i);
14+
pad(str.substring(1), ans+ch);
15+
}
16+
}
17+
public static void main(String[] args) {
18+
pad("12", "");
19+
}
20+
}

‎Recursion/Permutations.java

Copy file name to clipboard
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package Recursion;
2+
3+
import java.util.ArrayList;
4+
5+
public class Permutations {
6+
7+
private static void permutation(String str,String ans) {
8+
if(str.length() == 0) {
9+
System.out.println(ans);
10+
return;
11+
}
12+
char ch = str.charAt(0);
13+
for(int i = 0;i<=ans.length();i++) {
14+
String f = ans.substring(0, i);
15+
String e = ans.substring(i,ans.length());
16+
permutation(str.substring(1), f+ch+e);
17+
}
18+
}
19+
20+
private static ArrayList<String> permutationArrayList(String str,String ans) {
21+
if(str.length() == 0) {
22+
ArrayList<String> list = new ArrayList<>();
23+
list.add(ans);
24+
return list;
25+
}
26+
char ch = str.charAt(0);
27+
ArrayList<String> arr = new ArrayList<>();
28+
for(int i = 0;i<=ans.length();i++) {
29+
String f = ans.substring(0, i);
30+
String e = ans.substring(i,ans.length());
31+
32+
arr.addAll(permutationArrayList(str.substring(1), f+ch+e));
33+
}
34+
return arr;
35+
}
36+
37+
private static int permutationCount(String str,String ans) {
38+
if(str.length() == 0) {
39+
// System.out.println(ans);
40+
return 1;
41+
}
42+
int count = 0;
43+
char ch = str.charAt(0);
44+
for(int i = 0;i<=ans.length();i++) {
45+
String f = ans.substring(0, i);
46+
String e = ans.substring(i,ans.length());
47+
count+=permutationCount(str.substring(1), f+ch+e);
48+
}
49+
return count;
50+
}
51+
52+
public static void main(String[] args) {
53+
String str = "abc";
54+
permutation(str,"");
55+
System.out.println(permutationArrayList(str,""));
56+
System.out.println(permutationCount("abcd",""));
57+
58+
}
59+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.