Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

Briefly describe how even parity and odd parity work.

Answered: 1 week ago