Checking strings against each other (Anagrams)

后端 未结 23 1932
忘了有多久
忘了有多久 2020-11-30 10:55

The assignment is to write a program that accepts two groups of words from the user and then prints a \"True\" statement if the two are anagrams (or at least if all the lett

23条回答
  •  庸人自扰
    2020-11-30 11:00

    Just another solution without using sort:

    s1 = "aaabbbccc"
    s2 = "abcabcabc"
    
    def are_anagram1(s1, s2):
       return [False, True][sum([ord(x) for x in s1]) == sum([ord(x) for x in s2])]
    
    print are_anagram1(s1,s2)
    

    NB: this works only for alphabet not numerals

提交回复
热议问题