How to determine if two strings are anagrams?

First of all, what are anagrams? Well, anagrams are words that have the same characters which can be at different locations in the words. For example, “level” and “level” are anagrams, a special kind of anagrams because they are exactly the same in terms of characters and their locations in the words. Moreover, “rat” and “art” are also anagrams because all characters in “rat” are in “art”, no more no less. Another example is “seek” and “ekes”. Pay attention to the numbers of “e” letters in both words, they must be the same amount too. Thus, “seek” and “sek” are not anagrams.

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Linq;

   4: using System.Text;


   6: namespace areAnagrams {

   7:     class Program {

   8:         static bool areAnagrams(string str1, string str2) {

   9:             if (str1.Length != str2.Length)

  10:             {

  11:                 return false;

  12:             }


  14:             int[] charsTracker = new int[26];


  16:             for (int i = 0; i < str1.Length; i++)

  17:             {

  18:                 charsTracker[str1[i] - 'a']--;

  19:                 charsTracker[str2[i] - 'a']++;

  20:             }


  22:             for (int i = 0; i < 26; i++)

  23:             {

  24:                 if (charsTracker[i] != 0)

  25:                     return false;

  26:             }


  28:             return true;

  29:         }


  31:         static void Main(string[] args) {


  33:             if (areAnagrams("abcxyz", "zyxcba"))

  34:                 Console.WriteLine("True");

  35:             else

  36:                 Console.WriteLine("False");


  38:         }

  39:     }

  40: }


  1. First we check to see if the two strings have the same length. If not, they are not anagrams.
  2. Then, we declare an array of 26 integers. We will use this array to keep track of the characters in both strings. Why 26? You may ask. It is because we have 26 letters in the alphabet. Each of the letter is represented by an index in the array. And the value stored in each index represents the count of the letter’s occurrence in both words.
  3. Next, we just loop through the strings and count the number of occurrences for each letter in both strings. For every letter in the first string, we’ll subtract 1 from the count value stored in the index representing that letter. On the other hand, we’ll add 1 for every letter in the second string. By doing this, we know that if both strings have the same letters, the final result will always be 0 because they cancel each other out through decrement and increment.
  4. Lastly, we check the array of counts. If any index has a non-zero value, then the strings are not anagrams

We’ll do an example by checking for “rat” and “art”. First run of the loop, “r” count is -1 because it is in first string and “a” count is 1 because it is in second string. Second run, “a” count is now 0 because it appears in the first string so 1 – 1 is 0. Moreover, “r” count is also 0 because it appears in the second string -1 + 1 = 0. Last run, “t” count is 0 because it appears in both strings 1 – 1 = 0. Thus, we know “rat” and “art” are anagrams because the resulting array contains all 0s