Given two strings s and t, return true if t is an anagram of s, and false otherwise.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Constraints:
1 <= s.length, t.length <= 5 * 104s and t consist of lowercase English letters.Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To figure out if two words are anagrams of each other using the most straightforward approach, you can imagine making every single possible rearrangement of the first word. Then, you simply compare each of those creations against the second word to see if any of them match.
Here's how the algorithm would work step-by-step:
import itertools
def is_valid_anagram(first_word, second_word):
# Generate every possible permutation to check all letter rearrangement possibilities
all_permutations = itertools.permutations(first_word)
for current_permutation in all_permutations:
joined_permutation = "".join(current_permutation)
# Check every generated rearrangement against the target word for an exact match
if joined_permutation == second_word:
return True
# Return false if all possible permutations were exhausted without a match
return FalseInstead of comparing every single character of both words against each other from scratch, the optimal approach counts how many times each letter appears in the first word. Then, it uses those exact same counts to check off every matching letter in the second word.
Here's how the algorithm would work step-by-step:
def is_anagram(first_word, second_word):
if len(first_word) != len(second_word):
return False
# Different lengths cannot be anagrams, so we fail early to save computation time.
letter_frequency_map = {}
for current_character in first_word:
if current_character in letter_frequency_map:
letter_frequency_map[current_character] += 1
else:
letter_frequency_map[current_character] = 1
for current_character in second_word:
if current_character not in letter_frequency_map or letter_frequency_map[current_character] == 0:
return False
# We decrement to ensure every character from the second word cancels out the first.
letter_frequency_map[current_character] -= 1
# All counts must be perfectly exhausted to confirm both words are true anagrams.
return True| Case | How to Handle |
|---|---|
| Strings of different lengths | Return false immediately by checking if the lengths of s and t are equal at the start. |
| Empty strings | Return true because two empty strings are technically valid anagrams of each other. |
| Single-character strings that match or do not match | Compare the single characters directly or through the frequency count to return true or false accurately. |
| Strings with all identical characters | Frequency counter correctly increments and decrements the counts to ensure exact matching quantities. |
| Strings with Unicode characters or emojis | Use a hash map instead of a fixed-size array to support arbitrary character sets beyond standard ASCII. |
| Extremely large strings exceeding memory limits | Process the strings in a single pass using a compact frequency array to optimize space and time complexity. |
| Case sensitivity where uppercase and lowercase letters differ | Treat characters strictly as case-sensitive unless a normalization step is explicitly required by the prompt. |
| Completely disjoint character sets | The frequency array will encounter negative counts or unmatched non-zero entries, resulting in an early return of false. |