0% found this document useful (0 votes)
24 views2 pages

Solutions

The document contains a Java class named 'Solutions' that includes methods for printing digits as words, counting substrings in a string, generating subsequences of a string, and solving the Tower of Hanoi problem. The main method demonstrates the Tower of Hanoi solution with 3 disks. Each method utilizes recursion to achieve its functionality.

Uploaded by

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

Solutions

The document contains a Java class named 'Solutions' that includes methods for printing digits as words, counting substrings in a string, generating subsequences of a string, and solving the Tower of Hanoi problem. The main method demonstrates the Tower of Hanoi solution with 3 disks. Each method utilizes recursion to achieve its functionality.

Uploaded by

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

class Solutions{

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

public static void printDigits(int num){


//base case
if(num==0){
System.out.println();
return;
}
int i=num%10;

printDigits(num/10);
System.out.print(digits[i]+" ");

static int CountSubstrings(String str,int i,int j,int n){


if(n==1){
return 1;
}
if(n<=0){
return 0;
}
int res=CountSubstrings(str, i+1, j, n-1)+ CountSubstrings(str,
i, j-1, n-1)- CountSubstrings(str, i+1, j-1, n-2);
if(str.charAt(i)==str.charAt(j)){
res++;
}
return res;
}

//SubSequences of a String

public static void SubSequences(String str, int idx,String


newString){
if(idx==str.length()){
System.out.println(newString);
return;
}
char currchar=str.charAt(idx);

//Choice :

// to be
SubSequences(str,idx+1,newString+currchar);
// not to be
SubSequences(str,idx+1,newString);

//Tower Of HANOI

public static void towerofHanoi(int n, String src,String helper, String


dest){
if(n==1){

System.out.println("Disk "+ n +": "+src +" --> "+dest);

return;
}
towerofHanoi(n-1, src, dest, helper);
System.out.println("Disk "+ n +": "+src +" --> "+dest);
towerofHanoi(n-1, helper, src, dest);

public static void main(String[] args) {


int n=3;
towerofHanoi(n, "S", "H", "D");

}
}

You might also like