Question: containsSubSequence takes two Strings as input and returns a boolean: Returns true if the first input string contains all the characters of the second input

containsSubSequence takes two Strings as input and returns a boolean: Returns true if the first input string contains all the characters of the second input string, in order, but not necessarily consecutively.

> HW2.containsSubSequence("abracadabra", "abcd") true > HW2.containsSubSequence("abracadabra", "abdc") false

you must not use either break or continue in your code.

You are allowed to use the following methods from the Java API:

  • class String
    • length
    • charAt
  • class StringBuilder
    • length
    • charAt
    • append
    • toString
  • class Character
    • any method

I write something but it does not work and I have no idea where is wrong. I want to check the second String's first char with the first String's every char at first. If they are the same I will set i equals the first String's length to force the program jump of the second loop(since I can't use break). Then, the second char of the second string compares with the first String's every char... when finishing all comparison. if they are the same return true otherwise false.

public static boolean containsSubSequence(String string1, String string2){ String store1 = ""; for(int j = 0; j < string2.length(); j++){ for(int i = 0; i < string1.length(); i++){ if(string2.charAt(j) == string1.charAt(i)){ store1 += string1.charAt(i); i = string1.length(); } } } if(store1.equals(string2)){ return true; } return false; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!