Breaking Down Anagrams: The Uniform Approach
Let's take two very simple inputs: Mary
and Army
.
How do we know that these two strings are anagrams? Well, we can keep track of both their letters and see if they match. The big idea is how to do the tracking-- can we do it in a uniform way that's intuitive?
Common Ground: What Makes Them the Same?
To determine if two strings are anagrams, we need to establish if they contain the exact same collection of letters. Think of it as two different piles of building blocks—each block is a letter, and we want to verify if both piles are made from the same set of blocks.
The Difference Maker: Ordering
If the difference between two potential anagrams is just the ordering of their letters, then it should stand to reason that sorting the letters would make them look the same.
VoilĂ ! We've stumbled upon our core strategy: sort the strings and then compare.
Sensitivity Training: Case Matters (Or Does It?)
To make our comparison agnostic to letter casing, we'll convert all characters in both strings to lowercase.
Illustrated Concept
Here's a visual guide to help you understand:

The Code: Making It Happen
Step 1: Convert to Lowercase
First, let's bring both strings to common ground by converting them to lowercase.
1let a = str1.toLowerCase();
2let b = str2.toLowerCase();