finding if two words are anagrams of each other

后端 未结 22 1258
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 14:51

I am looking for a method to find if two strings are anagrams of one another.

Ex: string1 - abcde
string2 - abced
Ans = true
Ex: string1 - abcde
string2 - ab         


        
22条回答
  •  隐瞒了意图╮
    2020-11-27 15:19

    How about converting into the int value of the character and sum up :

    If the value of sum are equals then they are anagram to each other.

    def are_anagram1(s1, s2):
        return [False, True][sum([ord(x) for x in s1]) == sum([ord(x) for x in s2])]
    
    s1 = 'james'
    s2 = 'amesj'
    print are_anagram1(s1,s2)
    

    This solution works only for 'A' to 'Z' and 'a' to 'z'.

提交回复
热议问题