Knowee
Questions
Features
Study Tools

An anagram is a word or a phrase made by transposing the letters of another word or phrase; for example, "parliament" is an anagram of "partial men," and "software" is an anagram of "swear oft." Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation

Question

An anagram is a word or a phrase made by transposing the letters of another word or phrase; for example, "parliament" is an anagram of "partial men," and "software" is an anagram of "swear oft." Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python program that checks if two strings are anagrams of each other:

def is_anagram(str1, str2):
    # Remove white spaces and convert to lower case
    str1 = str1.replace(" ", "").lower()
    str2 = str2.replace(" ", "").lower()

    # Check if both strings have the same length
    if len(str1) != len(str2):
        return False

    # Create a count dictionary for each string
    count1 = {}
    count2 = {}

    # Count the frequency of each character in the first string
    for char in str1:
        if char in count1:
            count1[char] += 1
        else:
            count1[char] = 1

    # Count the frequency of each character in the second string
    for char in str2:
        if char in count2:
            count2[char] += 1
        else:
            count2[char] = 1

    # Check if both dictionaries are equal
    if count1 == count2:
        return True
    else:
        return False

# Test the function
print(is_anagram("parliament", "partial men"))  # Should print True
print(is_anagram("software", "swear oft"))  # Should print True

This program works by counting the frequency of each character in both strings and comparing these counts. If the counts are the same for all characters, then the strings are anagrams of each other.

This problem has been solved

Similar Questions

Given a string S. the task is to determine if there exists another string that is an anagram of S.An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example "seekg" is an anagram of "geeks".Example 1:Input: S = "ab"Output: YESExplanation:The string "ba" is not identical to S and is an anagram of S

Write a program to check two strings are anagram or not.

Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation

Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Given an array of strings strs, group the anagrams together. You can return the answer in any order.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.Note: Use Structure

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.