using System.Linq;
public static class Isogram
{
public static bool IsIsogram(string word)
{
var lowerLetters = word.ToLower().Where(char.IsLetter).ToList();
return lowerLetters.Distinct().Count() == lowerLetters.Count;
}
}
The steps for this solution are
-
ToLowerproduces a newstringwith its characters changed to lower case. That string is chained toWhere. -
Whereuses IsLetter to filter thestringso only Unicode letters get put into a List. So no hyphens nor apostrophes (nor anything else that is not a letter) will make it into theList. -
DistinctusesCountto get the number of unique letters in thelowerLettersList. - The function returns whether the number of distinct letters is the same as the number of all letters.
A string is an isogram if its number of distinct letters is the same as the number of all its letters.
- The word
Bravois an isogram because it has five distinct letters and five letters total. - The word
Alphais not an isogram becauseais considered to repeatA, so it has only four distinct letters but five letters total.
4th Sep 2024
·
Found it useful?