# Is the string a pangram?

The following code checks if a string is a pangram or not:

public static class StringExtensions
{
public static IEnumerable<char> EnglishAlphabet
{
get
{
for (char i = 'a'; i <= 'z'; i++)
{
yield return i;
}

yield break;
}
}

/// <summary>
/// Check if the source string is a pangram.
/// </summary>
/// <remarks>
/// The default dictionary is the english one.
/// </remarks>
public static bool IsPangram(this string source)
{
return source.IsPangram(EnglishAlphabet);
}

/// <summary>
/// Check if the source string is a pangram.
/// </summary>
/// <remarks>
/// A pangram is a string that uses every letter in the alphabet.
/// </remarks>
public static bool IsPangram(this string source, IEnumerable<char> alphabet)
{
var lowerAlphabet = new string(alphabet.ToArray()).ToLower();
IDictionary<char, bool> alphabetCharacters = lowerAlphabet.ToDictionary(c => c, c => false);
var lowerSource = source.ToLower();

foreach(char character in lowerSource)
{
alphabetCharacters[character] = true;
}

return !alphabetCharacters.Values.Contains(false);
}
}


Do you see anything wrong/weird in it? Any feedback (style, naming, performance) is more than welcome.

• no reason to recreate EnglishAlphabet nor tolower – paparazzo Sep 28 '16 at 2:36

### Use a set instead of a dictionary

The current algorithm sets the value to true for each character it finds, and at the end it checks if there is anything still unset. This is a bit tedious, and inefficient. Checking if any value is false is an $O(n)$ operation.

A simpler and more natural solution would be using a set data structure (HashSet in C#). You could add characters that you found, and in the end check the size of the set, and compare it with the size of the alphabet.

If the input may contain letters not in the alphabet, then before adding letters to the found set, don't forget to check the letter is actually part of the alphabet. (Thanks @tym32167 for the reminder.)

### Use a simple array instead of a dictionary

If the alphabet is always the English alphabet, then an interesting simple alternative is using a simple boolean array of size 'z' - 'a' + 1. As you iterate over the letters, you can derive the array index to use by letter - 'a'. It will be simpler and more storage efficient than using a dictionary.

### Generate the alphabet once

The English alphabet is regenerated on each call of IsPangram. This is unnecessary, as it never changes between calls. You could generate it once and reuse.

• I didn't think of using a set actually. And now the code is way more elegant also. Thanks. – Gentian Kasa Sep 27 '16 at 21:35
• And don't wait until the end. If it is at 26 then stop. – paparazzo Sep 28 '16 at 2:38
• You could add characters that you found => don't forget to add only those characters, which included in your alphabet. – tym32167 Sep 28 '16 at 11:04

Just a small point:

public static IEnumerable<char> EnglishAlphabet
{
get
{
for (char i = 'a'; i <= 'z'; i++)
{
yield return i;
}

yield break;
}
}


Strings already implement IEnumerable<char>. You could replace all this code with:

public readonly static IEnumerable<char> EnglishAlphabet = "abcdefghijklmnopqrstuvwxyz";

• This answer doesn't really help. The OP already knows that because he uses such a loop in his code foreach(char character in lowerSource) and I think it's easier to create an alphabet like he did then writing all letters by hand. – t3chb0t Sep 28 '16 at 10:45
• Yes, easier. But now every time EnglishAlphabet is accessed, this loop is going to be ran. The readonly static puts it directly into memory with basically no computation. Additionally, if the OP wants to say only check for vowels then this version would be easier to modify. – JD.B Sep 28 '16 at 12:27
• @JD.B with basically no computation because it's soooo important here ;-] if the OP wants to say only check for vowels why would he want to do it? he's checking strings for being panagrams and not containing vowels :-\ – t3chb0t Sep 28 '16 at 13:29

I would suggest that you consider using an immutable set:

static readonly ImmutableHashSet<char> alphabet =
ImmutableHashSet<char>.Empty.Union("abcdefghijklmnopqrstuvwxyz");
static bool IsPangram(string s)
{
return alphabet.Intersect(s.ToLower()).Count == alphabet.Count;
}


Keep it short and simple; use off-the-shelf parts.

• well, this I like ;-) – t3chb0t Sep 28 '16 at 15:36

As others have pointed out you only have to check the number of distinct characters in the sentence. A one liner doing that using linq could be:

public static bool IsPangram2(this string text, int alphabetLength = 26)
{
return text.ToUpper().Where(ch => char.IsLetter(ch)).Distinct().Count() >= alphabetLength;
}


As stated in a comment the above solution does not handle accented letters so a better solution may be:

private static string DefaultCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static bool IsPangram3(this string text, string validCharacters = null)
{
text = text.ToUpper();
validCharacters = validCharacters?.ToUpper() ?? DefaultCharacters;
return !validCharacters.Any(ch => !text.Contains(ch));
}

• This doesn't work with any kind of accented characters in the string "abcdeèéhijklmnopqrstuvwxyz".IsPangram2() -> true. Using ToUpper and ToLower for case insensitive comparisons is a bad practice. – Johnbot Sep 28 '16 at 8:01

Since you're (apparently) already changing to a version that uses a set (and its size) instead of a dictionary, I think it's also worth considering letting the user just pass the size of the alphabet they care about.

public static bool IsPangram(this string source)
{
return source.IsPangram(26);
}


You really only care about the size of the alphabet anyway, so you might as well just pass the size. In the specific case of the English alphabet, it's quick and easy to generate all the lower-case letters, but for many other alphabets, this is a more difficult task--for example, for French or Spanish lower-case letters, it looks like you need to specify around 8 separate ranges of characters apiece (but it seems safe to assume French and Spanish speakers know the number of letters in their alphabets just as well as English speakers do). In short, passing the number is a lot easier than passing all the elements (which you really don't care about anyway).

Make it short, make it regex:

public static bool IsPangram(this string input)
{
const int alphabetLength = 26;
return Regex.Matches(input, @"[a-z]", RegexOptions.IgnoreCase)
.Cast<Match>()
.Select(m => m.Groups[0].Value.ToLower())
.Distinct()
.Count() == alphabetLength;
}

• You should call Distinct with a case insensitive comparer instead of calling ToLower. – Johnbot Sep 28 '16 at 8:04
• It also can't be used in case the alphabet is different from the English one. – Gentian Kasa Sep 28 '16 at 8:12
• @GentianKasa you exaggerate, is it really so hard to adjust the regex for other languages and/or add another parameter? – t3chb0t Sep 28 '16 at 10:42
• @Johnbot thx for pointing this out ;-) – t3chb0t Sep 28 '16 at 10:47
• @t3chb0t the difficulty is in adapting the regex. For example, the alphabet can the English one, Russian, Chinese, Arabic, [0-9], [actg], and anything else one can think of. By using a regex it becomes much more difficult to understand which alphabet is being used. – Gentian Kasa Sep 28 '16 at 14:32

Without a line by line review
Just an implementation of what Janos answered
Give the check to Janos

    private HashSet<int> panaHash = new HashSet<int>();
int cI;
public bool IsPangram(string source)
{
panaHash.Clear();
foreach (char c in source.ToLower())
{
cI = (int)c;
if (cI < 97)   // not going to change in your life time
continue;
if (cI > 122)  // not going to change in your life time
continue;
if (panaHash.Count == 26)
return true;
}
return false;
}


ran some time test and this was the fastest

me 1924
RegX 27436
Lippert 4587
Question 2357

• Down votes what is the problem? I don't think you are going to do it faster than this. – paparazzo Sep 28 '16 at 12:59
• @t3chb0t It is not really the votes I am concerned about. I want to know what is the problem? – paparazzo Sep 28 '16 at 16:20
• @t3chb0t it is faster than yours by a fact of 10 – paparazzo Sep 28 '16 at 16:30
• I didn't test it but I believe you ;-) and I didn't DV you. I suggest you write a few more words about your code. Perhaps this is what they don't like. – t3chb0t Sep 28 '16 at 16:33
• It's nice that you put out an implementation, and benchmark results, it's just that none of this reviews the posted code by OP. As such, I'm afraid this is not an answer on Code Review. Do take a look at our how-to-answer page. – janos Sep 29 '16 at 5:17