Bit field with bitCount

आइसोग्राम
आइसोग्राम Scala में
import java.util.Locale

object Isogram:
  def isIsogram(s: String): Boolean =
    val letters = s.toLowerCase(Locale.ROOT).filter(c => 'a' <= c && c <= 'z')
    val seen    = letters.foldLeft(0)((bits, c) => bits | (1 << (c - 'a')))
    Integer.bitCount(seen) == letters.length

This approach uses the bits of an Int to record which letters are in the string. An Int has 32 bits, which is enough for the 26 letters: bit 0 stands for a, bit 1 for b, and so on up to bit 25 for z.

It starts by lowercasing the string with toLowerCase, so that A and a set the same bit. The filter() method keeps the characters from a through z, and drops the spaces and hyphens.

The foldLeft() method then goes through the letters. It starts with 0, a number with no bits set, and passes the bits collected so far and the next letter to its lambda. Subtracting 'a' from the letter gives its position in the alphabet, from 0 to 25. The shift left operator << moves the single bit of 1 to that position, and the bitwise OR operator | sets that bit in the collected bits. Setting a bit that is already set changes nothing, so a letter that is repeated still sets only one bit.

That is what the last line relies on. Integer.bitCount() returns the number of bits that are set, which is the number of different letters. If it is equal to the number of letters, then no letter is repeated.

Caution

The bits stand for the letters from a through z only, so the filter cannot be isLetter. A letter such as é or я is dropped like a space, and a word that repeats one of them would be taken for an isogram. The other three approaches detect repeated letters in words such as été.

22 नवंबर 2026 · क्या यह आपके काम आया?