finding if two words are anagrams of each other

后端 未结 22 1312
隐瞒了意图╮
隐瞒了意图╮ 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:05

    How about this?

    a = "lai d"
    b = "di al"
    sorteda = []
    sortedb = []
    for i in a:
        if i != " ":
            sorteda.append(i)
            if c == len(b):
                for x in b:
                    c -= 1
                    if x != " ":
                        sortedb.append(x)
    sorteda.sort(key = str.lower)
    sortedb.sort(key = str.lower)
    
    print sortedb
    print sorteda
    
    print sortedb == sorteda
    

提交回复
热议问题