Member-only story

Checking Anagram Strings with Python

Paresh Krishna Sharma
3 min readJul 6, 2023

--

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.

Example of generating anagrams
Example of generating anagrams

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…

--

--

Paresh Krishna Sharma
Paresh Krishna Sharma

Written by Paresh Krishna Sharma

Unleashing the Power of Code, Data, and Creativity ✨ | Software Engineer at Microsoft | LinkedIn : www.linkedin.com/in/itsparesh

No responses yet