TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

Trains

Problem statement, definition, constraints, and public examples.

Problem Statement

MATCHING GAMES OF DOMINOES

This section is identical in all three problems - you may skip it if you are
familiar with matching games of dominoes.

Games of dominoes use tiles as playing pieces. Each tile has two numbers on it
- one at each end. The range of the numbers on the tiles depends on the size of
the dominoes set: double-six sets use numbers zero through six, double-nine
sets use numbers zero through nine, and so on. Each set has exactly one tile
for each pair of numbers in the range - a double-six set contains 28 tiles 0-0
through 6-6, a double-nine set contains 55 tiles 0-0 through 9-9, and so on.
Tiles are symmetric - there is one tile for each pair of numbers, regardless of
the order of numbers (for example, the 3-5 tile is the same as the 5-3 tile).
Matching games of dominoes require building trains of tiles. A train is a group
of tiles arranged linearly in such a way that numbers facing each other on the
adjacent tiles match. For example, 1-2 2-3 3-0 0-0 0-6 is a train, while 1-2
3-4 is not a train.

PROBLEM STATEMENT

Implement a class Trains with a method checkTrain to verify validity of a
domino train constructed from tiles of one double-nine set. Your method should
return true when the train is valid, and false when the train is not valid.

DEFINITION
Class Name: Trains
Method Name: checkTrain
Parameters: String
Returns: boolean
Method signature (be sure your method is public):  boolean checkTrain(String
train);

String train is a non-empty String that consists of an even number of digits
'0' through '9', inclusive. The first two characters of the String represent
the first tile in the train; the next two characters represent the second tile,
and so on. TopCoder will check that the input String:
- does not contain characters outside the '0'-'9' range,
- is not empty, and its length does not exceed 50 characters,
- has an even number of characters.

A train is valid when numbers facing each other on all the adjacent tiles
match, and no tile is used more than once. All other trains are invalid.

NOTE
Your method should disregard symmetry of the tiles: train "1232" is considered
invalid, even though it consists of the same tiles as a valid train "1223".

EXAMPLES
- if train = "12233445", checkTrain should return true (the train is 1-2 2-3
3-4 4-5)

- if train = "12323445", checkTrain should return false (because the second
tile is flipped the wrong way)

- if train = "12253445", checkTrain should return false (because tiles two and
three do not match)

- if train = "1221", checkTrain should return false (because tile 1-2 is used
twice).

Definition

Class:
Trains
Method:
checkTrain
Parameters:
String
Returns:
boolean
Method signature:
boolean checkTrain(String param0)
(be sure your method is public)

Constraints

    Examples

      No public sample cases were explicitly marked for this problem in the original data.

      Back to all problems