TopcoderArchive
Archive
Algorithm problem

ItemizedList

Problem statement, definition, constraints, and public examples.

Problem Statement

You are in charge of writing some software to generate an itemized invoice. You have a raw list of items that were purchased, but the raw list is not very user friendly. Your code should determine how many of each item were purchased, and return a list of items and their quantity. You are given a String[] items, each element representing a single item. You must return a String[] listing, in alphabetical order, each item and its quantity. Each element of the return should be formatted as "item - quantity" (quotes added for clarity), where the quantity has no leading zeroes.

Definition

Class:
ItemizedList
Method:
generateList
Parameters:
String[]
Returns:
String[]
Method signature:
String[] generateList(String[] items)
(be sure your method is public)

Constraints

  • items will contain between 1 and 50 elements, inclusive.
  • Each element of items will contain between 1 and 50 lowercase letters ('a'-'z'), inclusive.

Examples

  1. {"apple", "orange", "apple", "banana", "apple", "orange"}
    Returns: {"apple - 3", "banana - 1", "orange - 2" }
    A grocery list of someone getting ready to make a fruit salad.
  2. {"apple", "apple", "apple", "apple"}
    Returns: {"apple - 4" }
    The itemized format is a lot more concise.
  3. {"apple", "orange", "banana"}
    Returns: {"apple - 1", "banana - 1", "orange - 1" }
    Each item only appears once.
  4. {"apple", "orange", "apple", "banana", "apple",
     "apple", "orange", "apple", "banana", "apple",
     "apple", "orange", "apple", "banana", "apple"}
    Returns: {"apple - 9", "banana - 3", "orange - 3" }
    Lots of items.
  5. {"apple"}
    Returns: {"apple - 1" }
    A very short list.
Back to all problems