Write a program to check two strings are anagram or not.
Question
Write a program to check two strings are anagram or not.
Solution 1
Sure, here is a Python program that checks if two strings are anagrams:
def check_anagram(str1, str2):
# Remove 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
# Compare the count dictionaries
if count1 == count2:
return True
else:
return False
# Test the function
print(check_anagram("listen", "silent")) # Should print True
print(check_anagram("hello", "world")) # Should print False
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.
Solution 2
Sure, here is a Python program that checks if two strings are anagrams:
def check_anagram(str1, str2):
# Remove 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
# Compare the count dictionaries
if count1 == count2:
return True
else:
return False
# Test the function
print(check_anagram('listen', 'silent')) # Returns: True
print(check_anagram('hello', 'world')) # Returns: False
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.
Similar Questions
Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation
Given 2 strings, check if they are anagrams. An anagram is a rearrangement of the letters of one word to form another word. In other words, some permutations of string A must be the same as string B.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line containing 2 space-separated strings.Output FormatFor each test case, print True/False, separated by a new line.Constraints10 points1 <= T <= 1001 <= len(S) <= 103'a' <= S[i] <= 'z'40 points1 <= T <= 1001 <= len(S) <= 105'a' <= S[i] <= 'z'ExampleInput4iamlordvoldemort tommarvoloriddleb hstop posthi heyOutputTrueFalseTrueFalse
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
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
Check whether the given two numbers are Anagram or not. Two numbers are said to be as anagram if both numbers formed with same digits.Input Format:Accept two integers from the userOutput Format:Anagram or Not
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.