TopcoderArchive
Archive
Algorithm problem

NamesList

Problem statement, definition, constraints, and public examples.

Problem Statement

You are working on setting up a new filing system, and need to know which k-th letter occurs most frequently among the names of your clients. Given String[] names, each element of which contains the name of a client, and int k, the 1-based index of the character to examine, return a String indicating the k-th letter that appears most often. Ignore any names containing less than k characters. If multiple k-th letters appear the same number of times, return the one among them that comes first alphabetically.

Definition

Class:
NamesList
Method:
popularInitial
Parameters:
String[], int
Returns:
String
Method signature:
String popularInitial(String[] names, int k)
(be sure your method is public)

Constraints

  • names will contain between 1 and 50 elements, inclusive.
  • Each element of names will contain between 1 and 50 uppercase letters ('A'-'Z'), inclusive.
  • k will be between 1 and 50, inclusive.
  • At least one element of names will contain at least k characters.

Examples

  1. {"ANNE", "BILL", "BOB"}
    1
    Returns: "B"
    Here, we just want the first character. There is 1 'A' and 2 'B's.
  2. {"ANNE", "BILL", "WILL"}
    2
    Returns: "I"
    Looking at the second character, we have 'N', 'I', 'I'.
  3. {"ANN", "BOB", "JAMES"}
    5
    Returns: "S"
    Only one name has five characters, and the fifth character is an 'S'.
  4. {"STEVE"}
    2
    Returns: "T"
    With only one name, this is pretty trivial.
  5. {"JILL", "WILL", "CAL"}
    3
    Returns: "L"
    All the same third letter.
  6. {"ANN", "ALEX", "BOB", "BILL"}
    1
    Returns: "A"
    Two 'A' and two 'B', so we break the tie by taking the earlier letter.
Back to all problems