Question

In: Computer Science

Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if...

Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if the 2nd string is a subsequence of the 1st string. If not, the method will return false.

An empty string is a subsequence of every string. This is because all zero characters of the empty string will appear in the same relative order in any string

This method must not contain any loops.

In java

Solutions

Expert Solution

import java.io.*;

class SubSequence
{
   static boolean isSubSequence(String str1, String str2, int m, int n)
   {
       if (m == 0)
           return true;
       if (n == 0)
           return false;
          
       if (str1.charAt(m-1) == str2.charAt(n-1))
           return isSubSequence(str1, str2, m-1, n-1);
       return isSubSequence(str1, str2, m, n-1);
   }
  
   public static void main (String[] args)
   {
       String str1 = " "; //ENTER YOUR STRINGS BETWEEN THE DOUBLE INVERTED COMMA
       String str2 = " ";
       int m = str1.length();
       int n = str2.length();
       boolean res = isSubSequence(str1, str2, m, n);
       if(res)
           System.out.println("TRUE");
       else
           System.out.println("FALSE");
   }
}


Related Solutions

Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Write recursive method to return true if a given array has element equal to employee emp,...
Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp, and returns...
write both non-recursive and recursive functions that take the strings from the user and display strings...
write both non-recursive and recursive functions that take the strings from the user and display strings in backwards in python
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and...
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and returns number of elements in the stack. The elements in the stack should not be changed after calling this method. please do it in java
Write a method that concatenates two strings passed as parameters.    Version #1 - assign to...
Write a method that concatenates two strings passed as parameters.    Version #1 - assign to the 1st parameter the concatenation of both parameters. Perform the concatenation using the + operator. We are assuming reference parameter work that way (they don't).    Version #2 - fine. return the value of the 1st param after concatenating to it the second parameter then.    Version #3 - Let's do it the proper way now, I say. Create a temporary String reference variable...
Write code for a short method that does the following: accepts two strings as parameters, first...
Write code for a short method that does the following: accepts two strings as parameters, first name, and last name; Outputs the following message, concatenated together in one line of output:
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list...
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list which has odd number of elements, then it deletes the element in the middle only. The method should receive only one parameter (the array list) The base case is when the array list has only one element, just remove that, otherwise, you need to remove the first one and the last one then call the method, then you need to add them again after...
Write a method called printIsMultiple that receives a pair of integers and prints true if the...
Write a method called printIsMultiple that receives a pair of integers and prints true if the second is a multiple of the first; false, otherwise
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT