🔍

Anagram Detection Methods

Jun 28, 2025

Overview

The lecture explains how to determine if two strings are anagrams, discusses multiple approaches including sorting and frequency counting, and shows how to optimize for time and space.

What Is an Anagram?

  • Two strings are anagrams if they have the same characters with the same frequency, but possibly in different orders.
  • Example: "abcd" and "dabc" are anagrams; order differs but character counts are identical.
  • Anagrams cannot have differing lengths or extra/missing characters.

Basic Approach: Sorting

  • Convert both strings into character arrays and sort them.
  • Compare sorted arrays; if equal, the strings are anagrams.
  • If the lengths differ, immediately return false.
  • Sorting both arrays has time complexity O(n log n) due to sorting algorithms like Tim sort.

Optimized Approach: Frequency Counting

  • Use a frequency array of size 26 (for lowercase English letters).
  • Traverse both strings simultaneously in a single loop:
    • For each character in string A, increment the corresponding count in the frequency array.
    • For each character in string B, decrement the corresponding count in the frequency array.
  • After traversal, check if all values in the frequency array are zero.
  • If any value is non-zero, the strings are not anagrams.
  • This approach runs in O(n) time and uses O(1) space.

Efficient Implementation Steps

  • Check if string lengths are equal; if not, return false.
  • Initialize a frequency array of size 26 for 'a' to 'z'.
  • Traverse both strings together, updating the frequency array as described.
  • Loop over the frequency array; if all elements are zero, return true, else return false.

Key Terms & Definitions

  • Anagram — Two strings with identical character counts arranged in any order.
  • Time complexity — Measurement of how the computation time of an algorithm scales with input size.
  • Frequency array — An array to track occurrences of each character.
  • O(n log n) — Time complexity for efficient sorting algorithms.
  • O(n) — Linear time complexity; algorithm runs in time proportional to input size.

Action Items / Next Steps

  • Practice implementing both the sorting and frequency-counting methods for anagram checking.
  • Review array manipulation and character-to-index mapping using ASCII values.
  • Prepare for questions on time and space complexity analysis.