Priya R
Updated date Nov 16, 2023
In this blog, we will learn how to detect anagrams in Python using two straightforward methods.

## Introduction:

Anagrams are a fascinating linguistic and mathematical concept that has captivated wordplay enthusiasts for centuries. An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once. For example, "listen" and "silent" are anagrams of each other. Detecting anagrams can be both a fun pastime and a valuable problem-solving skill. In this blog, we will explore two simple methods for checking anagram strings in Python.

## Method 1: Sorting and Comparing

One of the easiest and most efficient methods to check if two strings are anagrams is by sorting their characters and comparing the results. Here's a Python program that demonstrates this method:

``````def are_anagrams_method1(str1, str2):
# Remove spaces and convert both strings to lowercase
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()

# Sort the characters in both strings
sorted_str1 = sorted(str1)
sorted_str2 = sorted(str2)

# Compare the sorted strings
return sorted_str1 == sorted_str2

# Example usage
string1 = "listen"
string2 = "silent"
result = are_anagrams_method1(string1, string2)
print(f"{string1} and {string2} are anagrams: {result}")
``````

### Output:

``````listen and silent are anagrams: True
``````

In this method, we first remove any spaces and convert both input strings to lowercase to ensure case-insensitive comparison. Then, we sort the characters of both strings using the `sorted()` function. Finally, we compare the sorted strings to check if they are equal. If they are equal, the input strings are anagrams.

## Method 2: Using Character Frequency Count

Another approach to check if two strings are anagrams is by comparing the frequency of characters in each string. In this method, we create a character frequency dictionary for each string and compare them. Here's a Python program illustrating this method:

``````def are_anagrams_method2(str1, str2):
# Remove spaces and convert both strings to lowercase
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()

# Create character frequency dictionaries for both strings
freq_dict1 = {}
freq_dict2 = {}

# Populate the frequency dictionaries
for char in str1:
freq_dict1[char] = freq_dict1.get(char, 0) + 1
for char in str2:
freq_dict2[char] = freq_dict2.get(char, 0) + 1

# Compare the frequency dictionaries
return freq_dict1 == freq_dict2

# Example usage
string1 = "listen"
string2 = "silent"
result = are_anagrams_method2(string1, string2)
print(f"{string1} and {string2} are anagrams: {result}")
``````

### Output:

``````listen and silent are anagrams: True
``````

In this method, we again remove spaces and convert both input strings to lowercase for consistent comparison. We create two dictionaries, `freq_dict1` and `freq_dict2`, to store the frequency of each character in their respective strings. We then iterate through both strings, counting the frequency of each character and populating the dictionaries accordingly. Finally, we compare the frequency dictionaries to determine if the input strings are anagrams.

## Conclusion:

In this blog, we have explored two simple and effective methods to check for anagram strings in Python. The first method involved sorting the characters of the input strings and comparing them. The second method focused on character frequency counting. Both methods produced accurate results and can be applied in various scenarios.