Write a program in JAVA that takes in two words and then it

Write a program in JAVA that takes in two words, and then it recursively determines if the letters of the first word are contained, in any order, in the second word. If the size of the first word is larger than the second then it should automatically return false. Also if both strings are empty then return true.

YOU MAY NOT(!!!!!!!!!!):

Use any iterative loops. In other words, no for-loops, no while-loops, no do-while-loops, no for-each loops.

Use the string method .contains(String)

USE:

Use the string method .charAt(Index)

Hints:

Recursive methods generally attempt to solve a smaller problem then return the results of those in order to solve the larger one.

Think of how to do this with a loop, and use that to guide what parameters you’ll need for your recursive method.

Example Dialog:

Enter 2 words. I will determine if the letters of one is contained in the other

elf

self

They are contained!

Example Dialog 2:

Enter 2 words. I will determine if the letters of one is contained in the other

jerky

turkey

They are not contained

Example Dialog 3:

Enter 2 words. I will determine if the letters of one is contained in the other

asdf

fasted

They are contained!

SHOW OUTPUT AS WELL.

Solution

Program:

import java.util.Scanner;

public class Sequence {

   static boolean isSubSequence(String str1, String str2, int m, int n)
   {

       if(n==0)
           return false;
       if(m==0)
           return true;
   if (str1.charAt(m-1) == str2.charAt(n-1))
   {
       return isSubSequence(str1, str2, m-1, str2.length());
   }
   else
      
       return isSubSequence(str1, str2, m, n-1);
  
     
   }
   public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
       System.out.println(\"Enter 2 words. I will determine if the letters of one is contained in the other\");
       String s1=s.next();
       String s2=s.next();

       if(isSubSequence(s1, s2, s1.length(), s2.length()))
       {
           System.out.println(\"They Are Contained\");
       }
       else
       {
          
               System.out.println(\"They Are Not Contained\");
          
       }
   }
}

Output:

Enter 2 words. I will determine if the letters of one is contained in the other
elf
self
They Are Contained

Enter 2 words. I will determine if the letters of one is contained in the other
jerky
turkey
They Are Not Contained

Write a program in JAVA that takes in two words, and then it recursively determines if the letters of the first word are contained, in any order, in the second
Write a program in JAVA that takes in two words, and then it recursively determines if the letters of the first word are contained, in any order, in the second

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site