Unlocking Anagram Magic in Python: Checking String Similarity with Frequency Analysis

python @ Freshers.in

Anagrams, words or phrases formed by rearranging the letters of another, offer an intriguing challenge in programming. In this comprehensive guide, we will explore anagram detection in Python. Our goal is to create a function that checks if two strings are anagrams of each other by comparing the frequencies of their characters. We will provide step-by-step examples and real-world outputs to help you master this essential string manipulation technique.

Introduction to Anagram Detection

An anagram is a word or phrase formed by rearranging the letters of another word or phrase. Detecting anagrams involves comparing the characters and their frequencies in two strings to determine if they share the same set of characters in the same quantities.

Implementing Anagram Detection in Python

Let’s start by creating a Python function named are_anagrams that checks if two strings are anagrams. Here’s the code:

def are_anagrams(str1, str2):
    # Remove spaces and convert to lowercase
    str1 = str1.replace(" ", "").lower()
    str2 = str2.replace(" ", "").lower()
    # Check if the lengths of both strings are equal
    if len(str1) != len(str2):
        return False
    # Create dictionaries to store character frequencies
    char_count1 = {}
    char_count2 = {}
    # Populate char_count1
    for char in str1:
        if char in char_count1:
            char_count1[char] += 1
            char_count1[char] = 1
    # Populate char_count2
    for char in str2:
        if char in char_count2:
            char_count2[char] += 1
            char_count2[char] = 1
    # Compare the character frequencies
    return char_count1 == char_count2

In this function:

  • We remove spaces and convert both input strings to lowercase to ensure case insensitivity and eliminate spaces.
  • We check if the lengths of the processed strings are equal. If they are not, the strings cannot be anagrams, and we return False.
  • We create two dictionaries, char_count1 and char_count2, to store character frequencies for each string.
  • We populate char_count1 and char_count2 by iterating through the characters in the strings and updating their frequencies.
  • Finally, we compare the two dictionaries to check if they have the same character frequencies. If they do, the strings are anagrams, and we return True. Otherwise, we return False.

Using the are_anagrams Function

Now that we have our are_anagrams function, let’s use it to check if two strings are anagrams. Here’s an example:

str1 = "listen"
str2 = "silent"
if are_anagrams(str1, str2):
    print(f"'{str1}' and '{str2}' are anagrams.")
    print(f"'{str1}' and '{str2}' are not anagrams.")

When you run this Python code, it will output:

'listen' and 'silent' are anagrams.

Handling Edge Cases

It’s important to handle edge cases when working with anagram detection. For example, you should consider cases where one string is an empty string or when the input strings contain special characters or non-alphabetic characters.

Refer more on python here :

Author: user