Question

In: Computer Science

Write a program in JAVA that takes in two words, and then it recursively determines if...

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.

Solutions

Expert 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


Related Solutions

Write a java program using the following instructions: Write a program that determines election results. Create...
Write a java program using the following instructions: Write a program that determines election results. Create two parallel arrays to store the last names of five candidates in a local election and the votes received by each candidate. Prompt the user to input these values. The program should output each candidates name, the votes received by that candidate, the percentage of the total votes received by the candidate, and the total votes cast. Your program should also output the winner...
Using Java, write a program that takes in two integers from the keyboard called m and...
Using Java, write a program that takes in two integers from the keyboard called m and n, where m > n. Your program should print the first m natural numbers (m..1) downwards in n rows.
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
JAVA Write a program that checks the spelling of words in a document. This program uses...
JAVA Write a program that checks the spelling of words in a document. This program uses two text files: A dictionary file containing all known correctly spelled words, and A document to be spell-checked against the dictionary. As the document to be spell checked is read, each of its words is checked against the dictionary words. The program determines whether each word is misspelled. Misspelled words are recorded. is spelled correctly. Correctly spelled words are recorded and their frequency counted....
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
in Java, write a program that takes an input of 4 numbers and splits them into...
in Java, write a program that takes an input of 4 numbers and splits them into 4 separate lines. EXAMPLE: so if input is 1994 the output should be 1 9 9 4
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should be only 3) in it and prints out the number of photos in the gallery.
I. General Description In this assignment, you will create a Java program to search recursively for...
I. General Description In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT