Valid Anagram | LeetCode 242 | Python | Solution

Click here to see this problem on LeetCode. This problem’s difficulty level is easy.

Problem Overview

Simple problem. From the given two strings, we have to check the first one is an anagram of the second one. If it’s not true, then we will return False. Otherwise, True.

According to Wikipedia: 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.

For example, we have two strings: ‘abc’ and ‘bac‘. And the second string contains all of the characters from the first string. But the sequences are different.

Remember, if any character appears more than one time in a string. The other one has to contain the same number of appearances.

'aabc' == 'abac' => true
'aabc' == 'abbc' => false

Coding Part

To solve this problem, first, we check the length of both strings. If it doesn’t match, there is nothing more to do. We will return False.

If the length of both strings is equal, we will track the appearance of both strings’ characters in a dictionary.

In our first loop, we will track the first string’s character. If a character is already in the dictionary, we will increase the value by 1. Otherwise, we will add the character as key and ‘1‘ as a value.

After the first loop, we will run another loop to track the second string’s character. But this time, we will check a bit differently. If a character is already in the dictionary, instead of increasing the value, we will decrease it by 1. Otherwise, we will add the character as key and ‘1‘ as a value.

If it’s a valid anagram, all the values will be zero in the dictionary after the two loops.

And finally, using another loop, we can decide the true or false.

Let’s see the coding solution.

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        
        if len(s) != len(t):
            return False
        
        track = {}        
        
        for char in s:
            if char in track:
                track[char] += 1
            else:
                track[char] = 1
                
        for char in t:
            if char in track:
                track[char] -= 1
            else:
                track[char] = 1
                
        for key in track:
            if track[key] != 0:
                return False
                break
                
        return True
Example One

str1 = 'aabc'
str2 = 'abac'

After 1st loop
track = {'a': 2, 'b': 1, 'c': 1}
After 2nd loop
track = {'a': 0, 'b': 0, 'c': 0}

Example Two
str1 = 'aabc'
str2 = 'abbc'

After 1st loop
track = {'a': 2, 'b': 1, 'c': 1}
After 2nd loop
track = {'a': 1, 'b': -1, 'c': 0}

Shortcut solution of this problem.

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        
        if len(s) != len(t):
            return False
        
        return sorted(s) == sorted(t)
str1 = 'aabc'
str2 = 'abac'

sorted(str1) = 'aabc'
sorted(str2) = 'aabc'

sorted(str1) == sorted(str2) => true

I hope you got the idea about the solution.

Happy Coding.
Happy Learning.