Anagram Cleanse: Letters Only
๐ฆ Anagram Cleanse: Letters Only
Difficulty: Beginner Tags: strings, normalize, anagram Series: CS 101
Problem
Write a function that checks if two strings are anagrams of each other. An anagram means both strings contain the exact same letters, just rearranged.
Here's the catch: you need to ignore spaces, punctuation, and capitalization. Only letters matter!
Input
data = {'a': str, 'b': str}
A dictionary with two strings to compare.
Output
bool # True if they're anagrams, False otherwise
Constraints
- Each string can be up to 100,000 characters long
- Only consider letters a-z (case-insensitive)
- Ignore spaces, punctuation, numbers, and special characters
Examples
Example 1: Anagram Match
Input: {'a': 'Dormitory', 'b': 'Dirty room!!'}
Output: True
Why? Let's break it down:
- String a: "Dormitory" โ Remove capitals โ "dormitory" โ Letters only โ "dormitory"
- String b: "Dirty room!!" โ Remove capitals โ "dirty room!!" โ Letters only โ "dirtyroom"
Now compare the letters: - "dormitory" sorted: d, i, m, o, o, r, r, t, y - "dirtyroom" sorted: d, i, m, o, o, r, r, t, y
They match! So it's True.
Example 2: Not an Anagram
Input: {'a': 'Hello', 'b': 'Olelhh'}
Output: False
Why?
- String a: "Hello" โ "hello" โ Letters: h, e, l, l, o
- String b: "Olelhh" โ "olelhh" โ Letters: h, h, l, l, e, o
Count comparison: - "hello" has: 1 h, 1 e, 2 l's, 1 o - "olelhh" has: 2 h's, 1 e, 2 l's, 1 o
The h count doesn't match, so it's False.