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 * 104
s
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:
The brute force way to check if two words are anagrams is to try out every possible combination of letters from the first word and see if we can rearrange them to match the second word. It is like blindly trying every possibility until we find a match, or we run out of options.
Here's how the algorithm would work step-by-step:
def is_anagram_brute_force(first_word, second_word):
# Anagrams must have equal length
if len(first_word) != len(second_word):
return False
second_word_list = list(second_word)
for first_word_character in first_word:
character_found = False
for second_word_index in range(len(second_word_list)):
# Attempt to find character match in second_word_list
if first_word_character == second_word_list[second_word_index]:
second_word_list.pop(second_word_index)
character_found = True
break
# Return False if no character in the first word is found
if not character_found:
return False
# Words are anagrams if second_word_list is now empty
if not second_word_list:
return True
else:
return False
To determine if two words are anagrams of each other, we will count how many times each letter appears in each word. If the counts are identical for both words, then they are anagrams.
Here's how the algorithm would work step-by-step:
def are_anagrams(first_word, second_word):
if len(first_word) != len(second_word):
return False
# Use a dictionary to store letter counts for the first word.
first_word_letter_counts = {}
for letter in first_word:
first_word_letter_counts[letter] = first_word_letter_counts.get(letter, 0) + 1
# Use a dictionary to store letter counts for the second word.
second_word_letter_counts = {}
for letter in second_word:
second_word_letter_counts[letter] = second_word_letter_counts.get(letter, 0) + 1
# Check that the character counts are identical.
if first_word_letter_counts == second_word_letter_counts:
return True
return False
Case | How to Handle |
---|---|
s or t is null | Return false immediately as null strings cannot be anagrams. |
s or t is empty string | If both are empty, return true; if only one is, return false. |
s and t have different lengths | Return false immediately since anagrams must have equal length. |
s and t are identical strings | Should return true because a string is an anagram of itself. |
s and t contain unicode characters | The character frequency map should support unicode characters correctly. |
s and t contain only a few distinct characters, but many repetitions of each. | Frequency counter approach should still work efficiently in this scenario. |
Very large strings that may cause integer overflow in frequency counting if not using a large enough data type. | Use a data type for frequency counting (e.g., int, long) large enough to avoid overflow. |
Strings with mixed case characters ('a' vs 'A') | Convert both strings to lowercase or uppercase before comparing frequencies. |