public class PangramChecker {
private final static int LETTERS_IN_ALPHABET = 26;
public boolean isPangram(String input) {
return input.toLowerCase().chars()
.filter(Character::isLetter)
.distinct()
.count() == LETTERS_IN_ALPHABET;
}
}
This approach starts be defining a private
final
static
value for all 26
letters in the English alphabet.
It is private
because it does not need to be directly accessed from outside the class,
and it is final
because its value does not need to be changed once it is set.
It is static
because it doesn't need to differ between object instantiations, so it can belong to the class itself.
The input String
is lowercased and chained into the chars()
method.
Each character is passed as a primitive int
representing its Unicode codepoint to the filter()
method.
The filter()
passes each codepoint to the IsLetter()
method to filter in only letter characters.
Another method that could be used is isAlphabetic()
.
For the difference between isAlphabetic()
and isLetter()
, see here.
The the count()
of the distinct()
surviving codepoints are compared with the number of letters expected,
which for the English alphabet is 26
.
This works as long as the only letters in the input are in the English alphabet.
If the input was missing a
but had α
, then the distinct letters would be 26
but it would not be a Pangram for English.