using System;
using System.Linq;
public static class Pangram
{
private static readonly StringComparison xcase = StringComparison.CurrentCultureIgnoreCase;
public static bool IsPangram(string input)
{
return "abcdefghijklmnopqrstuvwxyz".All(c => input.Contains(c, xcase));
}
}
- This begins by setting a variable for a case-insensitive
string
comparison. - It then checks if all letters in the alphabet are contained in the input,
using the LINQ method
All
with theString
methodContains
. -
Contains
takes the optionalStringComparison
argument, but a case-insensitive comparison is about seven times slower than if the input were lowercased and then an exact comparison were done.
Shortening
When the body of a function is a single expression, the function can be implemented as an expression-bodied member, like so
public static bool IsPangram(string input) =>
"abcdefghijklmnopqrstuvwxyz".All(c => input.Contains(c, xcase));
or
public static bool IsPangram(string input) => "abcdefghijklmnopqrstuvwxyz".All(c => input.Contains(c, xcase));
6th Nov 2024
·
Found it useful?