TopcoderArchive
Archive
Algorithm problem

CompleteLastAnswer

Problem statement, definition, constraints, and public examples.

Problem Statement

You are doing a multiple choice test. Each question has exactly three options 'A', 'B' and 'C'. You came up with all the answers, except one. You have no idea how to respond to the remaining question, so you decided it was better to just choose the least used option among the other questions. If there are several tied choices, you will choose the alphabetically earliest one.

You will be given a String answers representing the current status of your answer sheet. It will contain all uppercase letters 'A', 'B' or 'C' and exactly one underscore, representing the last unanswered question. Return a String containing the final status of your answer sheet in the same format, but replacing the underscore with an appropiate answer, using the strategy described above.

Definition

Class:
CompleteLastAnswer
Method:
getAnswers
Parameters:
String
Returns:
String
Method signature:
String getAnswers(String answers)
(be sure your method is public)

Constraints

  • answers will contain between 1 and 50 characters, inclusive.
  • Each character of answers will be one of 'A', 'B', 'C' or '_'.
  • There will be exactly one underscore ('_') character in answers.

Examples

  1. "AB_ABCBCAB"
    Returns: "ABCABCBCAB"
    There are 3 answers A, 4 answers B and 2 answers C. C has the fewest appearances, so we select C as the answer for the remaining space.
  2. "ABCABCABCA_"
    Returns: "ABCABCABCAB"
    A has 4 occurrences, while B and C have 3 each. Since we break ties by selecting the first solution alphabetically, we select B.
  3. "ABCBBCBCBA_CBACBACBBB"
    Returns: "ABCBBCBCBAACBACBACBBB"
  4. "_"
    Returns: "A"
Back to all problems