Task
Write an implementation that returns whether a String
has vowels (English language) in alphabetical (A-Z) order or not.
Feedback
- Is my
vowels
variable appropriate? Is there a better way to implement a vowel lookup? - If a candidate
String
has no vowels, the validator will returntrue
- is that reasonable? From a logical perspective, the program isn't lying, but you could make the same argument if it returnedfalse
. Would it be better to return an exception - something likeNoVowelsInCandidateException
?
Implementation
public class VowelOrderValidator {
private static final List<Character> vowels = new ArrayList<>(
Arrays.asList('A', 'E', 'I', 'O', 'U', 'Y')
);
public static boolean areVowelsOrdered(final String candidate) {
final char[] chars = candidate.toUpperCase().toCharArray();
int lastVowelIndex = 0;
for (final char c : chars) {
final int lookupIndex = vowels.indexOf(c);
if (-1 != lookupIndex) {
if (lookupIndex < lastVowelIndex) {
return false;
} else {
lastVowelIndex = lookupIndex;
}
}
}
return true;
}
}
"[^aeiouy]*(?:a[^eiouy]*)?(?:e[^aiouy]*)?(?:i[^aeouy]*)?(?:o[^aeiuy]*)?(?:u[^aeioy]*)?y?[^aeiou]*"
(but I find it really less readable!) \$\endgroup\$