0% found this document useful (0 votes)
16 views3 pages

Recursion (Basics) Solutions

The document contains five solutions to basic recursion problems in Java. Each solution includes a class with methods to find all occurrences of a key in an array, print digits of a number, calculate the length of a string, count substrings, and solve the Tower of Hanoi problem. Additionally, a reference to a video discussing the solutions is provided.

Uploaded by

shobit8910
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Recursion (Basics) Solutions

The document contains five solutions to basic recursion problems in Java. Each solution includes a class with methods to find all occurrences of a key in an array, print digits of a number, calculate the length of a string, count substrings, and solve the Tower of Hanoi problem. Additionally, a reference to a video discussing the solutions is provided.

Uploaded by

shobit8910
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

RECURSION (Basics) SOLUTIONS

[email protected]
Solution 1:
public class Solution {
public static void allOccurences(int arr[], int key, int i) {
if(i == arr.length) {
return;
}

if(arr[i] == key) {
System.out.print(i+" ");
}
allOccurences(arr, key, i+1);
}
public static void main(String[] args) {
int arr[] = {3, 2, 4, 5, 6, 2, 7, 2, 2};
int key = 2;
allOccurences(arr, key, 0);
System.out.println();
}
}

Solution 2:
public class Solution {
static String digits[] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};

public static void printDigits(int number) {


if(number == 0) {
return;
}

int lastDigit = number%10;


printDigits(number/10);
System.out.print(digits[lastDigit]+" ");
}
public static void main(String[] args) {
printDigits(1234);
System.out.println();
}
}

[email protected]
Solution 3 :
public class Solution {
public static int length(String str) {
if(str.length() == 0) {
return 0;
}

return length(str.substring(1)) + 1;
}
public static void main(String[] args) {
String str = "abcde";
System.out.println(length(str));
}
}

Solution 4 :
public class Solution {
public static int countSubstrs(String str, int i, int j, int n) {
if (n == 1) {
return 1;
}
if (n <= 0) {
return 0;
}

int res = countSubstrs(str, i + 1, j, n - 1) +


countSubstrs(str, i, j - 1, n - 1) -
countSubstrs(str, i + 1, j - 1, n - 2);

if (str.charAt(i) == str.charAt(j)) {
res++;
}
return res;
}
public static void main(String[] args) {

[email protected]
String str = "abcab";
int n = str.length();
System.out.print(countSubstrs(str, 0, n-1, n));
}
}

Solution 5 :
public class Solution {
public static void towerOfHanoi(int n, String src, String helper, String dest) {
if(n == 1) {
System.out.println("transfer disk " + n + " from " + src + " to " + dest);
return;
}

//transfer top n-1 from src to helper using dest as 'helper'


towerOfHanoi(n-1, src, dest, helper);
//transfer nth from src to dest
System.out.println("transfer disk " + n + " from " + src + " to " + helper);
//transfer n-1 from helper to dest using src as 'helper'
towerOfHanoi(n-1, helper, src, dest);
}

public static void main(String args[]) {


int n = 4;
towerOfHanoi(n, "A", "B", "C");
}
}

The Solution for this particular question has also been discussed here :
https://www.youtube.com/watch?v=u-HgzgYe8KA
At timestamp : 00:05

You might also like