Data Structure
Hi, could you please help with the following question in Java
a. Suppose we have some List of Strings called list and a String prefix. Write a method that removes all the Strings from list that begin with prefix.
public static <E> void removePrefixStrings(List<String> list, String prefix) {
write your method here
}
b. What is the time complexity of the method you wrote?
Hi,
here i am providing sample code for above problem along with Sample output.
Sample Code:-
--------------------
package simplejavaapplication;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* main method to starts the execution of a program
*
* @author DELL
*/
public class StringApp {
public static void main(String[] args) {
// A prefix of a string is a substring of that occurs at the
beginning of String.
//Decalring list collection framework as of type String
List<String> strings = new LinkedList<>();
strings.add("Welcome to Chegg India");
strings.add("HomeworkLib India");
strings.add("HomeworkLib India Platform Welcomes You");
//let us take prefix as Chegg.
String prefix = "HomeworkLib";
removePrefixStrings(strings, prefix);
}
/**
* Method to remove all strings from the list that contains the
prefix
* "Chegg"
*
* @param strings
* @param prefix
*/
public static void removePrefixStrings(List<String> strings,
String prefix) {
System.out.println("Original String Tokens " +
strings.toString());
strings.forEach(StringToken -> {
System.out.println("Token " +
Arrays.toString(StringToken.split(prefix)));
});
}
}
Sample output:-
--------------------
run:
Original String Tokens [Welcome to Chegg India, HomeworkLib India, HomeworkLib
India Platform Welcomes You]
Token [Welcome to Chegg India]
Token [, India]
Token [, India Platform Welcomes You]
BUILD SUCCESSFUL (total time: 0 seconds)
Time Complexity for the above Solution is:
It takes O(1) to remove all prefixes from that string that matches with given list of Strings.
Since while comparing prefix with list of all strings initially will look at the first index of strings
So it wont compare with all tokens/words of a given list of String.
please
let me know if want any clarification.
QSearch (Ctrl+1) Navigator 4 11 SimpleJavaApplication - NetBeans IDE 8.0.2 File Edit View Navigate Source Refactor Run Debug Profile Team Tools Window Help 20 0 <default config> v R - Projects x Files Services StringApp.java x #- ICONacademy Source History ...079 PBBO N - - SimpleJavaApplication -@ Source Packages + 3 com.in.HomeworkLibprograms public class StringApp { simplejavaapplication SimpleJavaApplication.java public static void main(String[] args) { LG StringApp.java V/ A prefix of a string is a substring of that occurs at the beginning of String. + Libraries //Decalring list collection framework as of type String List<String> strings = new LinkedList<>(); strings.add("Welcome to Chegg India"); strings.add("HomeworkLib India"); strings.add("HomeworkLib India Platform welcomes You"); //let us take prefix as Chegg. String prefix = "HomeworkLib"; remove PrefixStrings (strings, prefix); * Method to remove all strings from the list that contains the prefix * "Chegg * @param strings * @param prefix public static void removePrefixStrings (List<String> strings, String prefix) { System.out.println("Original String Tokens " + strings.toString()); strings.forEach(StringToken -> { System.out.println("Token " + Arrays.toString (StringToken.split (prefix))); }); simplejavaapplication.StringApp >
Data Structure Hi, could you please help with the following question in Java a. Suppose we...
Week 6 6. (15 points) Suppose you have some List of S List of Strings called List and a String prefix. Write a method that removes all the Strings from list that begin with prefix. public void removePrefixStrings(List-String- list, String prefix) 7. (2 points) What is the time complexity of this algorithm? 17 points Page 5 of 10
Need Help Filling in Areas: /** Based on a Big Java problem You have to write a static method that removes every other element from a linked list. Expected output: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] [Monday, Wednesday, Friday] [Wednesday] [] */ public class DownSizeTester { public static void main(String[] args) { LinkedList list = new LinkedList() ; list.add("Sunday") ; list.add("Monday") ; list.add("Tuesday") ; list.add("Wednesday") ; list.add("Thursday") ; list.add("Friday") ; list.add("Saturday") ; System.out.println(list) ; downsize(list) ; System.out.println(list) ; downsize(list)...
PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...
create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...
Suppose we have the following Java Interface: public interface Measurable { double getMeasure(); // An abstract method static double average(Measurable[] objects) { // A static method double sum = 0; for (Measurable obj : objects) { sum = sum + obj.getMeasure(); } if (objects.length > 0) { return sum / objects.length; } else { return 0; } } } Write a class, called Person, that has two instance variables, name (as...
easy question but i am confused. someone help
Consider the following Java classes: class OuterClass { static class InnerClass { public void inner Method({ System.out.println("This is my inner class"); public static void outer Method{ System.out.println("This is my outer class"); public class OuterClass Test{ public static void main(String args[]) { Complete the class OuterClass Test to produce as output the two strings in class OuterClass. What are the outputs of your test class?
Hi, I was wondering if I could get some help on a java program that I am supposed to be writing. The code that I have can be found below. Thanks in advance. Here are the requirements of the code 1) Create 3 cube objects. The size of each of the cubes should be input from the keyboard (hint: study the code below) 2) Print the Side length, Surface area and Volume to the users screen (console) for each of...
Could somebody please help.!
Please DON't copy paste from other places and please read the
question and answer exactly what's asked.
Thank you I much appreciate it.
You are to write a program name MyArrayStack.java that create/build the ArrayStack data structure. The class must be written to accept any type of Objects. The following must be implemented i.e. YOU must write the code (do not import any stack from the Java Library): 1. One default constructor that will create an...
JAVA - Circular Doubly Linked
List
Does anybody could help me with this method below(previous)?
public E previous() {
// Returns the previous Element
return null;
}
Explanation:
We have this class with these two implemented
inferfaces:
The interfaces are:
package edu.ics211.h04;
/**
* Interface for a List211.
*
* @author Cam Moore
* @param the generic type of the Lists.
*/
public interface IList211 {
/**
* Gets the item at the given index.
* @param index the index....
Hi, there
I would like to get a answer for this one. Could you please help
me? It's java
thanks a lot
Write a method that takes an array of booleans, theArray, and returns the "Yes" if the last element is true and "Nope" otherwise The method signature should be: public String check (boolean[] theArray) For example: Test Result System.out.println(check(new boolean[]{true, true, true})); Yes System.out.println(check(new boolean[]{true, true, true, false})); Nope