Drill

ProblemsC# › warmup

Are these two an anagram

mediumwarmupHash mapsStringsSortingC#

A word game checks whether one phrase uses exactly the same letters as another.

IsAnagram(text: string, other: string) → bool

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public bool IsAnagram(string text, string other) {
    
}

Worked examples

CallResult
IsAnagram("Listen", "Silent")true
IsAnagram("a b", "ba")true
IsAnagram("hello", "world")false
IsAnagram("abc", "ab")false

Hint

Strip and lowercase both, then compare the sorted characters, or count them.

Reference solution in C#
public bool IsAnagram(string text, string other) {
    var a = text.ToLowerInvariant().Where(c => !char.IsWhiteSpace(c)).OrderBy(c => c).ToArray();
    var b = other.ToLowerInvariant().Where(c => !char.IsWhiteSpace(c)).OrderBy(c => c).ToArray();
    return a.SequenceEqual(b);
}

The same problem in another language

More warmup problems in C#