Set with subsetOf

パングラム
パングラム Scalaにて
import java.util.Locale

object Pangrams:
  private val alphabet = ('a' to 'z').toSet

  def isPangram(input: String): Boolean =
    alphabet.subsetOf(input.toLowerCase(Locale.ROOT).toSet)

This approach starts by defining the alphabet as a private value of the Pangrams object, so it is built only once. The to method creates the range of Chars from a through z, and toSet turns the range into a Set.

The isPangram() method lowercases the sentence with toLowerCase, so that A and a count as the same letter. Calling toSet on the result gives the set of all the different characters in the sentence. A set holds each of its elements only once, so the repeated characters disappear.

The subsetOf() method then checks that every element of alphabet is also an element of that set. The spaces, digits and punctuation marks in the sentence need no special handling: they are extra elements of the sentence's set, and extra elements do not stop the alphabet from being a subset of it.

Since the call to subsetOf() is the last expression of the method, its value is returned without needing the return keyword.

2026年11月22日(日) · 役に立ちましたか?