Question

Data Structure Hi, could you please help with the following question in Java a. Suppose we...

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?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 >

Add a comment
Know the answer?
Add Answer to:
Data Structure Hi, could you please help with the following question in Java a. Suppose we...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Week 6 6. (15 points) Suppose you have some List of S List of Strings called...

    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

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    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....

    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...

    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 {...

    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?

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    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....

  • Could somebody please help.! Please DON't copy paste from other places and please read the question...

    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] Please test your code in the link I provide before you post your answer. The...

    [Java] Please test your code in the link I provide before you post your answer. The output should be looked like exact same as the tester. http://www.codecheck.it/files/17050415451csldwjahxt2kwn73ahd6vukt Thank you. Write a simplified application to illustrate the use of an undo button for a word processor. It keeps a history of all items and allows the user to undo the last. Write a class UndoStack. Implement using a Stack of Strings. The constructor will create an empty Stack to keep the...

  • Hi, there I would like to get a answer for this one. Could you please help...

    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

  • [Java] Please test your code in the link I provide before you post your answer. The...

    [Java] Please test your code in the link I provide before you post your answer. The output should be looked like exact same as the tester. http://www.codecheck.it/files/17033122188mcxvjz8n8qbk0k9fyfrd3w95 Use the following file: LinkedListUtilTester.java import java.util.LinkedList; public class LinkedListUtilTester { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); list.add("6"); list.add("7"); list.add("8"); list.add("9"); list.add("10"); list.add("11"); list.add("12"); list.add("13"); list.add("14"); list.add("15"); LinkedListUtil.shrink(list, 3); System.out.println(list); System.out.println("Expected: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]"); System.out.println(LinkedListUtil.reverse(list)); System.out.println("Expected:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT