PROBLEM STATEMENT
A chord progression is a sequence of symbols representing chords, delimited by
the '-' character. Write a method that determines if a chord progression is
valid, according to the following rules:
- A progression must be in either a major or minor key
If the progression is in a major key, the only allowable chord symbols are
(quotes are for clarification only):
The tonic chord: "I"
The dominant chords: "viio", "V"
The predominant chords: "ii", "IV"
The six chord: "vi"
If the progression is in a minor key, the only allowable chord symbols are:
The tonic chord: "i"
The dominant chords: "viio", "V"
The predominant chords: "iio", "iv"
The six chord: "VI"
- The last symbol must be a tonic chord
- No single chord may appear 3 or more times in a row
- A dominant chord must be immediately followed by a tonic chord, a six chord,
or another dominant chord
- A predominant chord must be immediately followed by a dominant chord or
another predominant chord
- A six chord must be immediately followed by a predominant chord or another
six chord
- A tonic chord may be followed by any chord
DEFINITION
Class: Composer
Method Name: isValid
Parameters: String
Returns: boolean
Method signature (be sure your method is public): boolean isValid(String
chords);
NOTES
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- chords contains between 1 and 50 characters, inclusive
- chords contains only valid chord symbols and the character '-'. chords will
begin and end with a valid chord symbol, and consecutive chord symbols will be
separated by a single '-'. The valid chord symbols are "i", "I", "ii", "iio",
"iv", "IV", "V", "vi", "VI", and "viio".
Examples:
"iv-V-I" returns false because it is not in either a major or minor key
"I-I-I" returns false because the "I" chord is repeated 3 times in a row
"V-viio-V-I" returns true. Even though there are 3 dominant chords in a row,
no single chord is repeated 3 times in a row
"I-IV-V" returns false because the last symbol is not a tonic chord
"I-IV-I" returns false because a predominant chord is immediately followed
by a tonic chord
"I-I-IV-V-I" returns true