Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f6a32e

Browse files
committed
some advanced recursion questions
1 parent 158c3a6 commit 5f6a32e

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

src/binod/CountPaths.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package binod;
2+
public class CountPaths {
3+
public static int countPaths(int i, int j, int n, int m) {
4+
if (i == n || j == m) {
5+
return 0;
6+
}
7+
if (i == n - 1 && j == m - 1) {
8+
return 1;
9+
}
10+
//move downwords
11+
int downPaths = countPaths(i + 1, j, n, m);
12+
//move right
13+
int rightPaths = countPaths(i, j + 1, n, m);
14+
return downPaths + rightPaths;
15+
}
16+
public static void main(String[] args) {
17+
int n = 3, m = 3;
18+
int totalPaths = countPaths(0, 0, n, m);
19+
System.out.println(totalPaths);
20+
}
21+
}

src/binod/KeyPad.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package binod;
2+
public class KeyPad {
3+
public static String[] keypad = {".", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tu", "vwx", "yz"};
4+
public static void printComb(String str, int idx, String combination) {
5+
if (idx == str.length()) {
6+
System.out.println(combination);
7+
return;
8+
}
9+
char currChar = str.charAt(idx);
10+
String mapping = keypad[currChar - '0'];
11+
for (int i = 0; i < mapping.length(); i++) {
12+
printComb(str, idx + 1, combination + mapping.charAt(i));
13+
}
14+
}
15+
public static void main(String[] args) {
16+
String str = "23";
17+
printComb(str, 0, "");
18+
}
19+
}

src/binod/PrintPermutations.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package binod;
2+
public class PrintPermutations {
3+
public static void printPerm(String str, String permutation) {
4+
if (str.length() == 0) {
5+
System.out.println(permutation);
6+
return;
7+
}
8+
for (int i = 0; i < str.length(); i++) {
9+
char currChar = str.charAt(i);
10+
//"abc" -> "ab"
11+
String newStr = str.substring(0, i) + str.substring(i + 1);
12+
printPerm(newStr, permutation + currChar);
13+
}
14+
}
15+
public static void main(String args[]){
16+
String str="abc";
17+
printPerm(str, "");
18+
}
19+
}

src/binod/UniqueSubSequences.java

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

0 commit comments

Comments
 (0)