Question

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public void removePrefixStrings(List<String> list , String prefix) {
if(list==null || list.size()==0)
return;

for(int i=0; i<list.size();) {
if(list.get(i).startsWith(prefix))
list.remove(i);
else
++i;

}

}


TimeComplexity : If prefix is same as String length, Then finding all prefix match will take n *n = n2
Then removal is also n

So the total time complexity is O(n3)

Add a comment
Know the answer?
Add Answer to:
Week 6 6. (15 points) Suppose you have some List of S List of Strings called...
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
  • 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?

  • Suppose we define a class called "Something" that maintains a list of objects of some type....

    Suppose we define a class called "Something" that maintains a list of objects of some type. Suppose further that we declare methods "doA" and "doB" in the class. Method "doA" has a void return type and takes an object received as an input argument and adds it to the end of the list. Method "doB" removes the object that is in the first position in the list and returns it. What type of abstract data structure does the class represent?

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • Help will be highly rated and gratefully appreciated. Stuck with this question: I have a linked...

    Help will be highly rated and gratefully appreciated. Stuck with this question: I have a linked list that stores string at each node. Now I need help with these: 1. public String findSmallestAndRemove() The findSmallestAndRemove method should search the smallest string (using compareTo method of the String class), and removes all of such smallest strings in the linked list, and return the smallest string. It should return null if the linked list is empty. 2. public int countHowManyStringsWithGivenLength(int length) The...

  • Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String a...

    Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String argument. The method will return a new String by removing the logest segment that contains consequtively repeating characters. public static void main(String []args){ String s=”1111222223333311111111444455552222”; String res= removeLongestRepeatingSegment(s); will return “11112222233333444455552222” } public static String removeLongestRepeatingSegment(String s){ --------------- Q2)15p)Given a list of objects stored (in ascending order) in a sorted linked list, implement member method which performs search as efficiently as possible for an object based on a key....

  • DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an...

    DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an unknown number of cells filled with data. As before, the array will already be filled with strings, some of which contain the string "Angela". You will create a method called countItUp which returns and integer array containing the number of times each of the letters in my name occur within the strings within this array. For example, if the input array was wordFun =...

  • DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an...

    DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an unknown number of cells filled with data. As before, the array will already be filled with strings, some of which contain the string "Angela". You will create a method called countItUp which returns and integer array containing the number of times each of the letters in my name occur within the strings within this array. For example, if the input array was wordFun =...

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due...

    Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due Friday, February 9th, 2017 @ 11:59PM EST Directions Create a List object. Using the following definition (List.h file is also in the repository for your convenience) for a list, implement the member functions (methods) for the List class and store the implementation in a file called List.cpp. Use an array to implement the list. Write the client code (the main method and other non-class...

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