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:
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;
}
Program Code to Copy:
public class Subsequences {
public static boolean containsSubSequence(String string1, String string2) {
int i, j, flag;
//To store indexes of first occurences of characters in string2 consecutively
int indexes[] = new int[string2.length()];
for (i = 0; i < string2.length(); i++) {
flag = 0;
for (j = 0; j < string1.length(); j++) {
if (string1.charAt(j) == string2.charAt(i)) {
//Storing the index if index found
indexes[i] = j;
flag = 1;
j = string1.length();
}
}
//Return false if element not found
if (flag == 0)
return false;
}
//Checking if indexes is strictly increasing i.e index of current character is not greater than index of next character(valid subsequence)
for (i = 0; i < indexes.length - 1; i++) {
if (indexes[i] > indexes[i + 1])
return false;
}
//Returning true of indexes is increasing
return true;
}
public static void main(String[] args) {
System.out.println(containsSubSequence("abracadabra", "abcd"));
System.out.println(containsSubSequence("abracadabra", "abdc"));
}
}
Code Screenshot:


Output Screenshot:

containsSubSequence takes two Strings as input and returns a boolean: Returns true if the first input...
Programming project in Java: 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 Create a class called HW2 that contains the following methods: 1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...
Need help with these two questions
String outputBreadthFirstSearch(): returns a string represenng a
breadth first traversal. So, for example, for the tree shown in
Figure 1, the method should output the string "bcaahttaetersse" 4.
String outputDepthFirstSearch(): returns a string represenng a pre
order depth first traversal. So, for example, for the tree shown in
Figure 1, the method should output the string "batcathateersse
This is my code so far
public class Trie { final TrieNode root; public Trie() {
this.root...
Do anyone knows how to write this in java? 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 moveXDownLeft takes a char and a two dimensional array of char as input and returns nothing: The method should find the first occurrence of the char in the array (searching from "top" to "bottom" and "left" to "right"), and it should slide that character...
Chapter 8 Exercise 36, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY. 8.36 (Latin square) A Latin square is an n-by-n array filled with n different Latin letters, each occurring exactly once in each row and once in each column. Write a program that prompts the user to enter the number n and the array of characters, as shown in the sample output, and checks if the input array is a Latin square. The characters are the first n...
In Java Only can use class String length charAt class StringBuilder length charAt append toString class Character any method Create the following methods nthWord takes an int and a String as input and returns a String: The input int represents a number n that is assumed to be positive, and the output string contains every nth word of the input string, starting with the first word, separated by a single space. {\em For this method, a word is defined to...
Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) ^ rowPuzzle.java:16: error: class, interface, or enum expected } //Java Program import java.util.*; // rowPuzzle helper function implementation // File: rowPuzzle.cpp // Header files section // function prototypes public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) { // base case // return true if the puzzle is solvable if (index == squares.size() - 1) { return...
Java:
Create the skeleton.
Create a new file called ‘BasicJava4.java’
Create a class in the file with the appropriate name.
public class BasicJava4 {
Add four methods to the class that return a default value.
public static boolean isAlphabetic(char aChar)
public static int round(double num)
public static boolean useSameChars(String str1, String
str2)
public static int reverse(int num)
Implement the methods.
public static boolean isAlphabetic(char aChar):
Returns true if the argument is an alphabetic character, return
false otherwise. Do NOT use...
PYTHON The function longest that returns the longest of two strings. The function count_over that takes a list of numbers and an integer nand returns the count of the numbers that are over n. The function bmi that takes two parameters height and weight and returns the value of the body mass index: a person's weight in kg divided by the square of their height in meters. def longest(string1, string2): """Return the longest string from the two arguments, if the...
*JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> { private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10; public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0;...
In Java All methods listed below must be public and static. If your code is using a loop to modify or create a string, you need to use the StringBuilder class from the API. Keep the loops simple but also efficient. Remember that you want each loop to do only one "task" while also avoiding unnecessary traversals of the data. No additional methods are needed. However, you may write additional private helper methods, but you still need to have efficient...