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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started