Access over 35 million academic & study documents

A) Implement the following method to return all strings in an alphab

Content type
User Generated
Rating
Showing Page:
1/3
a) Implement the following method to return all strings in
an alphabetically sorted array that start with a given prefix.
For instance, given a prefix bi, the returned strings are
Bill Clinton, Bill Gates, and Bill Joy. Note that all
string comparisons should be case INSENSITIVE. The
strings in the returned list must be in the order in which
they appear in the array. Your implementation must be as
efficient as possible - if not, you will get AT MOST HALF
the credit. Assume that the array has no duplicate entries.
If there are no matches, you may either return null, or an
empty array list. You may use the following String methods
(in addition to any others you may recall):
boolean startsWith(String s)
int compareTo(String s)
int compareToIgnoreCase(String s)
String toLowerCase(String s)
String toUpperCase(String s)
(As for ArrayList, you only need to use the add method to
add an item to the end of the array list.)
You may write helper methods (with full implementation) as
necessary. You may not call any method that you have not
implemented yourself.
public static ArrayList
prefixMatch(String[] list, String prefix) { // COMPLETE
THIS METHOD
b) Analyze the worst case big O running time of your
implementation: define the parameters that will
figure in your big O result, specify the operations you are
counting toward the running time, and show your
derivation. (If your algorithm uses another algorithm we
have studied in class, you can use the big O result
for the other algorithm without deriving it.)
**FOR THIS PROBLEM YOU ARE WRITING CODE FOR
ONLY THE METHOD AND ANALYZING THE BIG(O) AND
EXPLAINING HOW YOU GOT THE BIG(O). HAS TO BE

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/3
THE MOST EFFICIENT PROGRAM**
Solution
public static ArrayList<String> prefixMatch(String[] list,
String prefix) {
if(list == null || prefix==null)
return null;
ArrayList<String> finallist = new ArrayList<String>();
int prefixLen = prefix.length(); // getting length of
prefix: O(k): k is length of prefix
for(int i=0; i<list.length; i++){ // iterating over all
strings in list: O(n): n is number of string
// getting first k character from current string: O(k)
String subStr = list[i].substring(0, prefixLen);
// comparing prefix and first k character of string :
O(k)
if(prefix.equalsIgnoreCase(subStr))

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/3

Sign up to view the full document!

lock_open Sign Up
Unformatted Attachment Preview
a) Implement the following method to return all strings in an alphabetically sorted array that start with a given prefix. For instance, given a prefix “bi”, the returned strings are “Bill Clinton”, “Bill Gates”, and “Bill Joy”. Note that all string comparisons should be case INSENSITIVE. The strings in the returned list must be in the order in which they appear in the array. Your implementation must be as efficient as possible - if not, you will get AT MOST HALF the credit. Assume that the array has no duplicate entries. If there are no matches, you may either return null, or an empty array list. You may use the following String methods (in addition to any others you may recall): boolean startsWith(String s) int compareTo(String s) int compareToIgnoreCase(String s) String toLowerCase(String s) String toUpperCase(String s) (As for ArrayList, you only need to use the add me ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Awesome! Made my life easier.

Studypool
4.7
Indeed
4.5
Sitejabber
4.4

Similar Documents