TopcoderArchive
Archive
Algorithm problem

CellPhoneLookup

Problem statement, definition, constraints, and public examples.

Problem Statement

Nearly every cell phone comes equipped with a directory storing names and numbers. You will write a function to aid in data retrieval. A user will input a String query containing part of a name, or part of a number. You will return all directory entries which contain the given query as a substring (case-sensitive). This includes the cases where query is the entire name or number of a particular entry. The given directory is a String[] whose elements are formatted as "name number". The returned String[] should contain all pertinent elements of directory, in the same order they occur in directory.

Definition

Class:
CellPhoneLookup
Method:
getRecord
Parameters:
String[], String
Returns:
String[]
Method signature:
String[] getRecord(String[] directory, String query)
(be sure your method is public)

Constraints

  • directory will contain between 1 and 50 elements inclusive.
  • Each element of directory will contain between 3 and 50 characters inclusive.
  • Each element of directory will have the format (quotes for clarity) "name number" where,name is a positive length string of letters (a-z,A-Z) andnumber is a positive length string of digits (0-9).
  • query will contain between 1 and 50 characters inclusive.
  • query will contain only digits (0-9) or only letters (a-z,A-Z).

Examples

  1. {"BBBaBBB 1234567","a 1","AAAAAA 2"}
    "a"
    Returns: { "BBBaBBB 1234567",  "a 1" }
    The first and second names contain "a".
  2. {"Albert 6274237","Frank 16347788901","Tony 9236678"}
    "Albert"
    Returns: { "Albert 6274237" }
    Only the first entry matches.
  3. {"Albert 6274237","Frank 16347788901","Tony 9236678"}
    "78"
    Returns: { "Frank 16347788901",  "Tony 9236678" }
    The last two entries both match.
  4. {"Alfred 999999999999999999999999999999999",
     "Alfred 999999999999999999999999999999999",
     "Alfred 999999999999999999999999999999999",
     "Alfred 999999999999999999999999999999999"}
    "Fred"
    Returns: { }
    None match.
  5. {"Alfred 999999999999999999999999999999999",
     "Alfred 999999999999999999999999999999999",
     "Alfred 999999999999999999999999999999999",
     "Alfred 999999999999999999999999999999999"}
    "99"
    Returns: { "Alfred 999999999999999999999999999999999",  "Alfred 999999999999999999999999999999999",  "Alfred 999999999999999999999999999999999",  "Alfred 999999999999999999999999999999999" }
    All match.
Back to all problems