242. Valid Anagram#
Difficulty: Easy
Link to Problem: To see the Valid Anagram problem on LeetCode, click here!
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Constraints:
1 <=
s.length,t.length<= \(5 * 10^4\)sandtconsist of lowercase English letters.
Follow-up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
Solution:#
Here’s a Python function to solve this problem:
def isAnagram(s: str, t: str) -> bool:
# Check if the lengths of s and t are not equal
if len(s) != len(t):
return False
# Create dictionaries to store character frequencies for s and t
countS, countT = {}, {}
# Count character frequencies in s
for i in range(len(s)):
countS[s[i]] = 1 + countS.get(s[i], 0)
# Count character frequencies in t
for i in range(len(t)):
countT[t[i]] = 1 + countT.get(t[i], 0)
# Check if the character frequencies in s and t are the same
return countS == countT
Explanation:#
The code defines a class called
Solutionwith a methodisAnagramthat takes two input strings,sandt, and returns a boolean (TrueorFalse) based on whethertis an anagram ofs.The first check performed in the method is whether the lengths of
sandtare equal. If they are not equal, it immediately returnsFalsebecause strings of different lengths cannot be anagrams of each other.Two dictionaries,
countSandcountT, are created to store the frequency of characters insandt, respectively. These dictionaries will be used to count the occurrences of each character in the strings.The code then enters a loop to iterate through each character in string
susing aforloop. Inside the loop, it updates thecountSdictionary. The linecountS[s[i]] = 1 + countS.get(s[i], 0)increments the count of characters[i]incountSby 1. If the character is not already in the dictionary, it initializes the count to 1.Similarly, the code iterates through each character in string
tand updates thecountTdictionary in the same way.After counting the character frequencies in both strings, the code compares the two dictionaries using
countS == countT. If the dictionaries are equal, it means that both strings have the same character frequencies, and therefore,tis an anagram ofs. In this case, the method returnsTrue.If the dictionaries are not equal, the method returns
False, indicating thattis not an anagram ofs.
In summary, this code efficiently determines whether two input strings are anagrams by counting the character frequencies in each string using dictionaries and then comparing these counts. If the character frequencies match, the strings are considered anagrams, and the method returns True; otherwise, it returns False.
Test cases#
# Example 1:
s = "anagram"
t = "nagaram"
print(isAnagram(s, t))
True
# Example 2
s = "rat"
t = "car"
print(isAnagram(s, t))
False
Time and Space Complexity Analysis#
Let’s analyze the time and space complexity of the provided code:
Time Complexity:
The code first checks whether the lengths of the input strings
sandtare equal, which takes constant time. This check has a time complexity of O(1).The code then iterates through both strings,
sandt, once to count the character frequencies. Since both strings have a maximum length of 5 * 10^4, the worst-case scenario is that the code iterates through 5 * 10^4 characters in each string. This results in a linear time complexity of O(n), where n is the length of the longer of the two input strings (sort).Finally, the code compares the two dictionaries
countSandcountTto check if the character frequencies match. This comparison takes O(n) time in the worst case, where n is the length of the longer string.
Overall, the time complexity of the code is O(n), where n is the length of the longer input string.
Space Complexity:
The code uses two dictionaries,
countSandcountT, to store the character frequencies of the input stringssandt. In the worst case, both dictionaries can contain all unique characters from the input strings. Since the input strings can have a maximum length of 5 * 10^4, the space complexity for these dictionaries is O(5 * 10^4) or simply O(n), where n is the length of the longer input string.The code uses a few additional variables for bookkeeping, but these variables have constant space requirements and do not depend on the input size. Therefore, they do not significantly impact the overall space complexity.
In summary, the space complexity of the code is O(n), where n is the length of the longer input string.
Challenging Exercises:#
Anagram Chains: Given a list of words, find the longest chain of anagrams, where each word in the chain is an anagram of the previous word. For example, given [“bat”, “tab”, “cat”, “tac”, “dog”], the longest anagram chain is [“bat”, “tab”, “cat”, “tac”].
Minimum Deletions to Make Anagrams: Given two strings
sandt, find the minimum number of character deletions required in both strings to make them anagrams of each other. For example, ifs = "hello"andt = "billion", you can remove the characters “heo” fromsand “billi” fromtto make them anagrams (“lo” and “on” are left in the two strings).