October 13, 2024

Python Program to Find Anagrams

An anagram is a word or phrase that can be formed by rearranging the letters of another word or phrase, typically using all the original letters exactly once. For example, “listen” and “silent” are anagrams. Below is a Python program that checks whether two given strings are anagrams of each other.

Method 1: Using Sorting

One simple way to determine if two strings are anagrams is by sorting the characters in both strings and comparing them. If the sorted versions of the strings are identical, then the strings are anagrams.

Example: Anagram Checker Using Sorting

def are_anagrams(str1, str2):
    # Remove spaces and convert to lowercase
    str1 = str1.replace(" ", "").lower()
    str2 = str2.replace(" ", "").lower()

    # Sort and compare
    return sorted(str1) == sorted(str2)

# Example usage
string1 = "listen"
string2 = "silent"

if are_anagrams(string1, string2):
    print(f'"{string1}" and "{string2}" are anagrams.')
else:
    print(f'"{string1}" and "{string2}" are not anagrams.')
    

Output:

"listen" and "silent" are anagrams.
    

Method 2: Using a Character Count

Another efficient way to check for anagrams is by counting the frequency of each character in both strings and comparing these counts. If the character counts match, the strings are anagrams.

Example: Anagram Checker Using Character Count

from collections import Counter

def are_anagrams(str1, str2):
    # Remove spaces and convert to lowercase
    str1 = str1.replace(" ", "").lower()
    str2 = str2.replace(" ", "").lower()

    # Compare character counts
    return Counter(str1) == Counter(str2)

# Example usage
string1 = "Dormitory"
string2 = "Dirty room"

if are_anagrams(string1, string2):
    print(f'"{string1}" and "{string2}" are anagrams.')
else:
    print(f'"{string1}" and "{string2}" are not anagrams.')
    

Output:

"Dormitory" and "Dirty room" are anagrams.
    

Method 3: Using Default Dictionaries

This method involves creating a dictionary to count the occurrences of each character in both strings. If the dictionaries are equal, then the strings are anagrams.

Example: Anagram Checker Using Default Dictionary

from collections import defaultdict

def are_anagrams(str1, str2):
    # Remove spaces and convert to lowercase
    str1 = str1.replace(" ", "").lower()
    str2 = str2.replace(" ", "").lower()

    # Initialize dictionaries to count characters
    count1 = defaultdict(int)
    count2 = defaultdict(int)

    # Count characters in the first string
    for char in str1:
        count1[char] += 1

    # Count characters in the second string
    for char in str2:
        count2[char] += 1

    # Compare the dictionaries
    return count1 == count2

# Example usage
string1 = "The eyes"
string2 = "They see"

if are_anagrams(string1, string2):
    print(f'"{string1}" and "{string2}" are anagrams.')
else:
    print(f'"{string1}" and "{string2}" are not anagrams.')
    

Output:

"The eyes" and "They see" are anagrams.
    

Conclusion

Anagrams are a common concept in string manipulation, and Python provides multiple ways to check for anagrams efficiently. Whether you use sorting, character counting, or dictionaries, the goal is to ensure that the two strings contain the same characters in the same frequency. Each method has its advantages, and you can choose the one that best suits your needs.