Skip to main content

Anagram

An anagram is word switch or word play. It is the result of rearranging the letters of a word or phrase to produce a new word or phrase, while using all the original letters only once.

In interviews, usually we are only bothered with words without spaces in them.

ToLet's determinebreak ifdown twothe three common approaches in very simple terms:

  1. Sorting Both Strings

    • How it works:
      Rearrange (sort) the letters of both strings alphabetically. If the sorted versions are exactly the same, then the strings are anagrams, there are a few approaches:

        anagrams.
      • Time Cost:
        Sorting bothtakes stringsabout shouldO(n producelog n) time, where n is the number of characters.
      • Space Cost:
        It typically uses extra space proportional to the depth of recursion (about O(log n)).
      • Simple Analogy:
        Imagine you have two mixed-up decks of cards. If you sort them by suit and number, and they end up in the same resultingorder, string.then
          both
        • Thisdecks takesoriginally O(n.log(n))had timethe andsame O(log(n)) space.cards.
      • If we

        Prime mapMultiplication Method

        • How it works:
          Assign each character toletter a unique prime number and(like wea secret code). For each string, multiply eachthe mappedprimes numbercorresponding together,to anagramsits shouldletters. Thanks to the unique nature of prime factors, two strings will have the same multipleproduct (primeif factorand decomposition).only
            if they have the exact same letters.
          • ThisTime Cost:
            You only go through the string once, so it takes O(n) timetime.
          • Space Cost:
            It uses constant extra space, O(1), since the mapping of letters to primes is fixed.
          • Simple Analogy:
            Think of it like each letter is a unique ingredient with a special "number." Mix them all together (multiply), and O(1)if space.two Examples:recipes Group(strings) Anagramyield the same "flavor number," they contain exactly the same ingredients.
        • Frequency countingCounting

          of
            characters
          • How willit helpworks:
            Count tohow determinemany iftimes twoeach letter appears in each string using a hash (dictionary). Then, compare these counts. If they match for every letter, the strings are anagrams.
            • Time Cost:
              This also takes O(n) timetime, as you just make one pass through each string.
            • Space Cost:
              It uses constant extra space, O(1), because the number of letters (for example, 26 for the English alphabet) is fixed.
            • Simple Analogy:
              Imagine you have two baskets of fruit. If you count the number of apples, oranges, etc., in both baskets and O(1)the space.counts match exactly, then both baskets have the same mix of fruits.

Each method has its tradeoffs between speed and space:

  • Sorting is straightforward but slightly slower due to the sorting process.
  • Prime multiplication is fast and space-efficient, but it can run into issues with very long strings because the multiplication might produce huge numbers.
  • Frequency counting is both fast and efficient, and it's often the simplest and most reliable method.