Member-only story
Checking Anagram Strings with Python
Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, act and tac are an anagram of each other.
An anagram is a word or phrase formed by rearranging the letters of another word or phrase. In this article, we will explore how to determine whether two given strings are anagrams of each other using Python. We will provide a solution along with the corresponding time and space complexity analysis.
Approach
Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, act and tac are an anagram of each other.
Implementation
def check_anagram(a, b):
# Remove whitespaces and convert strings to lowercase
a = a.replace(" ", "").lower()
b = b.replace(" ", "").lower()
# Check if the lengths of strings are equal
if len(a) != len(b):
return False
# Create dictionaries to store character frequencies…