TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

DTMF

Problem statement, definition, constraints, and public examples.

Problem Statement

Dual Tone Multi-Frequency, or DTMF, is a method for instructing a telephone switching system of the telephone number to be dialed. Write a method that decodes a DTMF representation of a phone number. Return a special String "error" when the number is not encoded according to the DTMF rules.

This problem uses the following simplified description of the DTMF encoding:

  • - DTMF code represents digits 0 through 9, inclusive, and two special characters, '*' and '#', using a combination of exactly two of the seven pre-selected frequencies.
  • - The seven frequencies used for DTMF are divided in two groups - the high group of three frequencies, and the low group of four frequencies.
  • - Each combination of one frequency from the high group and one frequency from the low group represent a digit or a special character, as specified in the table below:
  •    | H1 H2 H3
    ---+---------
    L1 | 1  2  3
    L2 | 4  5  6
    L3 | 7  8  9
    L4 | *  0  #
    

H1, H2, and H3 represent the frequencies from the high group; L1, L2, L3, and L4 represent the frequencies from the low group. For example, a combination H1,L3 represents 7; H3,L4 represents '#', etc.

Each element of the input, frequencies, represents a pair of frequencies that encodes a digit or a special character. Elements are formatted as "F1,F2" (here and below quotations marks are used for clarity). The valid values for F1 and F2 are "H1", "H2", "H3", "L1", "L2", "L3", and "L4". For example, String "H1,L1" corresponds to digit "1". Note that frequencies within the pair are not ordered; for example, "L1,H3" and "H3,L1" represent the same digit.

Definition

Class:
DTMF
Method:
decode
Parameters:
String[]
Returns:
String
Method signature:
String decode(String[] frequencies)
(be sure your method is public)

Notes

  • Although TopCoder checks validity of each individual frequency, the input may still contain pairs of frequencies that do not encode a digit or a special character. For example, "L1,L4" is not a legal pair, even though both L1 and L4 are valid frequencies. When frequencies contains one or more illegal pairs, your method should return the "error" String (see examples).

Constraints

  • frequencies contains between 0 and 50 elements, inclusive
  • Each element of frequencies is exactly five characters long, and is formatted as "F1,F2"
  • Each value of F1 and F2 is from this list: "H1", "H2", "H3", "L1", "L2", "L3", and "L4". No other values for F1 and F2 are allowed.

Examples

  1. {"H1,L1","H2,L3","H2,L4","H2,L4","H2,L1","H3,L2","H2,L2","H2,L2","H3,L1","H2,L1","H2,L3"}
    Returns: "18002655328"
  2. {"L1,H1","L3,H2","L4,H2","L4,H2","L1,H2","L2,H3","H2,L2","H2,L2","H3,L1","H2,L1","H2,L3"}
    Returns: "18002655328"
  3. {"L1,L2"}
    Returns: "error"
  4. {}
    Returns: ""
Back to all problems