logo Practice-It logo

commonFirstLetters

Language/Type: Java Collections Sets and Maps
Author: Marty Stepp (on 2010/12/28)

Write a method commonFirstLetters that accepts a list of strings as a parameter, and returns a Set of strings. Your method should examine the strings in the list passed and return a set of all first letters that occur more than once. In other words, if two or more strings in the list begin with a particular character (case-insensitively), that letter should be part of the set that you return.

Consider a list variable called list containing the following elements:

["hi", "how", "is", "He", "Marty!", "this", "morning?", "fine.", "?huh?", "HOW", "I"]

One word in the list begins with "f", four begin with "h", two begin with "i", two begin with "m", one begins with "t", and one begins with "?". Therefore the set you return should contain the following elements:

["h", "i", "m"]

If no first character occurs 2 or more times, return an empty set. The order the elements appear in the set does not matter. You may assume that the list passed is not null and that none of its elements are null or empty strings.

Obey the following restrictions in your solution:

  • You may create only up to two (2) new data structures of your choice (list, stack, queue, set, map, etc.) as auxiliary storage. (You can have as many simple variables as you like.)
  • Your solution should run in less than O(N2) time, where N is the number of strings in the list.
  • You should not modify the contents of the list passed to your method.
Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.