TopcoderArchive
Archive
Algorithm problem

ItemSearch

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a String[], items, each element of which represents a single retail item and is formatted as "<description> <price>" (quotes for clarity only). <description> contains a textual description of the item and consists of only lowercase letters ('a'-'z') and spaces (but no leading or trailing spaces). <price> represents the item's price, and is formatted as "$D.CC" (quotes for clarity only), where D is the number of dollars (an integer between 0 and 1000, inclusive, with no extra leading zeros) and CC represents the number of cents (an integer between 00 and 99, inclusive, with exactly 2 digits).

Given a search term consisting entirely of lowercase letters ('a'-'z'), return all the items whose descriptions contain the search term as a substring, sorted in ascending order by price. The constraints will guarantee that no two items will have the same price. The return value is a String[] whose elements are formatted exactly the same as items.

Definition

Class:
ItemSearch
Method:
search
Parameters:
String[], String
Returns:
String[]
Method signature:
String[] search(String[] items, String searchTerm)
(be sure your method is public)

Constraints

  • items will contain between 1 and 50 elements, inclusive.
  • Each element of items will be formatted as described in the statement.
  • Each <description> will be between 1 and 30 characters in length, inclusive.
  • No two elements of items will have the same <price>
  • searchTerm will contain between 1 and 30 characters, inclusive.
  • searchTerm will contain only lowercase letters ('a'-'z').

Examples

  1. {"photographic lens $99.99",
     "autographed photo $4.00",
     "tent $88.88",
     "photo $0.25"}
    "photo"
    Returns: {"photo $0.25", "autographed photo $4.00", "photographic lens $99.99" }
    The three descriptions that contain "photo" as a substring are: "photographic lens", "autographed photo", and "photo".
  2. {"nothing you want $1000.99"}
    "stuff"
    Returns: { }
    An empty String[] is returned when there are no matches.
  3. {"same thing $0.00",
     "same thing $0.02",
     "same thing $0.01"}
    "thing"
    Returns: {"same thing $0.00", "same thing $0.01", "same thing $0.02" }
    Multiple items can have the same description.
Back to all problems