PROBLEM STATEMENT
Create a method that takes three arguments, a word, a set of consonants, and a
set of vowels. The method will examine the word and return its type based on
the letters contained in the set of consonants and the set of vowels using the
following definitions and rules.
A letter is defined as any character appearing in either or both of the set of
consonants or the set of vowels. A consonant is defined as any character
appearing in the set of consonants. A vowel is defined as any character
appearing in the set of vowels. A semi-vowel is defined as any character that
appears in both the set of consonants and the set of vowels. A semi-vowel can
behave as either a vowel or a consonant depending on its position within a
word. The same semi-vowel may behave as both a vowel and a consonant when
appearing more than once within the same word. A full-vowel is defined as a
vowel that is not also a semi-vowel.
A diphthong is defined as two consecutive vowels appearing within a word. The
first vowel must be a full-vowel. The second vowel may be either a full-vowel
or semi-vowel. The two vowels must differ. Depending on the context within a
word, a full-vowel followed by a semi-vowel may be treated as either a
diphthong or a vowel followed by a consonant.
A vowel-sound is either a full-vowel, semi-vowel, or diphthong.
The rules for determining a valid word are as follows:
W1. A word consists of 1 to 24 letters inclusive. As defined above, a letter is
any character appearing in either or both of the set of consonants or the set
of vowels.
W2. Every word must be either a simple word as defined below in rules S1
through S5, or a compound word as defined by rules C1 and C2.
S1. Every simple word must contain at least one vowel-sound.
S2. Every simple word must contain at least one letter that is not a semi-vowel.
S3. Every diphthong in a simple word must be either preceded or followed by a
consonant.
S4. A word can start with either a vowel-sound or consonant. Vowel-sounds and
consonants must alternate throughout the word.
S5. A letter may not appear consecutively within a simple word.
C1. A compound word consists of two or more valid simple words concatenated
together using all of the letters of the original word without any
rearrangement of the letters.
C2. Each simple word in a compound word must be a valid simple word of at least
two letters in length.
The method examines the word using the above definitions and rules and returns
the following.
If the word is not valid, return 0.
If the word is a valid simple word return 1.
If the word is a valid compound word, return the number of simple words
contained within the word.
When the word is valid, the value returned should always be the minimum
possible value. That is, if a word is both a valid simple word and a valid
compound word, return 1. If the word is compound, return the minimum number of
simple words that can be used to form the compound.
DEFINITION
Class Name: WordChecker
Method Name: wordType
Parameters: String, String, String
Returns: int
Method signature (be sure your method is public): int wordType(String word,
String consonants, String vowels);
TopCoder will ensure that each of the three inputs meet the following criteria:
- The characters in the strings are a-z (lowercase a through z).
- The strings are less than or equal to 26 characters in length.
NOTE
* The set of consonants and the set of vowels may contain duplicate characters.
The duplicates can be ignored. For example, the set "aaeeeiiouuuu" is
equivalent to the set "aeiou". The order of the characters is not significant.
* A full-vowel followed by a semi-vowel can be interpreted as either a
diphthong, in which case, as with all diphthongs, rule S3 applies, or simply as
a vowel followed by a consonant, in which case rule S3 is not involved. For
example, given the consonants "rst" and the vowels "aer", the word "art" is a
valid simple word. The "ar" is a diphthong followed by a consonant. The word
"are" is also a valid simple word. The "a" is a vowel, the "r" is a consonant.
EXAMPLES
wordType("star", "rstv", "aerv") ==> 0
The set of consonants is "rstv" and the set of vowels is "aerv". There are four
consonants: r, s, t, v. There are four vowels: a, e, r, v. There are two
full-vowels a, e. There are two semi-vowels r, v. There are six possible
diphthongs: ae, ar, av, ea, er, ev. There are ten possible vowel-sounds: a, e,
r, v, ae, ar, av, ea, er, ev.
The word "star" begins with two consecutive consonants violating rule S4 so it
is not a valid simple word. There is no way to break the word apart into two or
more simple words. Therefore, the word is not a valid compound word. The method
returns 0.
wordType("tease", "rstv", "aerv") ==> 1
The word "tease" is a valid simple word consisting of a consonant, a diphthong,
a consonant, and a vowel. The method returns 1.
wordType("raster", "rstv", "aerv") ==> 2
The word "raster" is not a valid simple word because of rule S4. But it can be
broken into "ras" and "ter" which are both valid simple words (both consonant,
vowel, consonant) and so "raster" is a valid compound word consisting of two
simple words. The method returns 2.
wordType("", "rstv", "aerv") ==> 0
wordType("r", "rstv", "aerv") ==> 0
wordType("r", "", "aerv") ==> 1
wordType("ae", "rstv", "aerv") ==> 0
wordType("rve", "rstv", "aerv") ==> 1
wordType("attest", "rstv", "aerv") ==> 0
wordType("taste", "rrstv", "aaerv") ==> 2
wordType("tastier", "rstv", "aerv") ==> 0
wordType("eaeatras", "rstv", "aerv") ==> 0
wordType("rattatattatat", "rstv", "aerv") ==> 3
wordType("rstrstrstrat", "rstv", "aerv") ==> 5
wordType("rarararararararararararar", "rstv", "aerv") ==> 0 (see rule W1)