filter with distinct

Pangramma
Pangramma a(z) Scala kurzusban
import java.util.Locale

object Pangrams:
  def isPangram(input: String): Boolean =
    input.toLowerCase(Locale.ROOT).filter(c => 'a' <= c && c <= 'z').distinct.length == 26

This approach is a chain of calls, each of which works on the result of the one before it.

It starts by lowercasing the sentence with toLowerCase, so that A and a count as the same letter.

The filter() method keeps only the characters for which its lambda returns true. Chars can be compared with <=, so the lambda keeps the characters from a through z and drops the spaces, digits and punctuation marks.

The distinct method removes the repeated characters, keeping the first occurrence of each. What is left is a String with one character for each different letter in the sentence. If its length is 26, then every letter of the alphabet is in the sentence.

Caution

The filter has to be exactly the range from a through z. The isLetter method looks like a shorter way to write it, but it is also true for letters such as é and я. Those would be counted too, so a sentence with only 25 letters of the alphabet and one é would be taken for a pangram.

2026. november 22. · Hasznosnak találtad?