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 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
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...
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 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:
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...
5. Write a method to accept five int parameters and return the largest of the five.  ...
5. Write a method to accept five int parameters and return the largest of the five.         Example: largest(45, 13, 65, 19, 23) returns 65. Java Program (METHOD ONLY!!!)
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and...
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and "end" (an int). It should return the sum (total) of all of the integers between (and including) "start" and "end". If "end" is less than "start", the function should return -1 instead. e.g. if you give the function a start of 10 and an end of 15, it should return 75 (i.e. 10+11+12+13+14+15)
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT