Skip to content

Anagram check

C# character counting

Flowchart (ISO 5807)

YesNoYesNoStartInput a, b|a| ≠ |b|Return falseEndcount = new int[256]i = 0, |a| − 1, 1count[a[i]] = count[a[i]] + 1count[b[i]] = count[b[i]] − 1c = 0, 255, 1count[c] ≠ 0Return falseEndReturn trueEndFigure 1 — Program.IsAnagram

Source code

C#
class Program {
    static bool IsAnagram(string a, string b) {
        if (a.Length != b.Length) {
            return false;
        }
        int[] count = new int[256];
        for (int i = 0; i < a.Length; i++) {
            count[a[i]]++;
            count[b[i]]--;
        }
        for (int c = 0; c < 256; c++) {
            if (count[c] != 0) {
                return false;
            }
        }
        return true;
    }
}

The editor opens with this code — change it and the chart redraws.

Language
C#
Constructs
character counting
Topic
Algorithms
Formats
Word, Visio, draw.io, PDF, SVG

Related examples