Reputation: 31
The input of this function is two strings, and the aim is to judge whether these two strings are anagrams. For example, "qwerty" and "qetyrw" are anagrams, which means rearrange the characters in first string can get the second string, case-insensitively. "qwerty" and "QWerTY" are also anagrams. I'm just confused with my function, which returns nothing.
My function is as follows:
def is_anagram(string_a,string_b):
"""returns True if the strings are anagrams of each other
str, list -> boolean"""
new_a=string_a.lower()
new_b=string_b.lower()
i=0
if len(string_a)!=len(string_b):
return False
else:
while i<=len(new_a)-1:
if new_a[i] in new_b:
list(new_b).remove(new_a[i])
i=i+1
break
if len(list(new_b))==0:
return True
else:
return False
Upvotes: 0
Views: 1397
Reputation: 20015
Sorting to identify if a string is anagram of another, takes O(n logn)
time. Instead, we can use a counter object that counts the occurrences of each letter:
from collections import Counter
def is_anagram(string_a, string_b):
return Counter(string_a.lower()) == Counter(string_b.lower())
This will take on average linear time. The basic observation is that two anagram strings have exactly the same letter frequencies.
Examples:
>>> is_anagram("abc", "aaa")
False
>>> is_anagram("abc", "aac")
False
>>> is_anagram("abc", "acb")
True
Upvotes: 5
Reputation: 5508
Your code is wrong by "list(new_b)", it is not stored, so every time it will be regarded as a new list.
And the "break" in the while loop is also not necessary.
Please try the following code:
def is_anagram(string_a,string_b):
"""returns True if the strings are anagrams of each other
str, list -> boolean"""
new_a=string_a.lower()
new_b=string_b.lower()
i=0
if len(string_a)!=len(string_b):
return False
else:
list_b = list(new_b) # store the list(new_b) into list_b
while i<=len(new_a)-1:
if new_a[i] in list_b:
list_b.remove(new_a[i])
i=i+1
#break should be removed!
if len(list_b)==0:
return True
else:
return False
Upvotes: 0
Reputation: 64298
You can use the Counter
class:
from collections import Counter
def is_anagram(string_a,string_b):
return Counter(string_a.lower()) == Counter(string_b.lower())
Upvotes: 1
Reputation: 15423
You just have to sort the strings:
def is_anagram(string_a, string_b):
return sorted(string_a.lower()) == sorted(string_b.lower())
Upvotes: 3