finding if two words are anagrams of each other

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

    I guess your sorting algorithm is not really O(log n), is it?

    The best you can get is O(n) for your algorithm, because you have to check every character.

    You might use two tables to store the counts of each letter in every word, fill it with O(n) and compare it with O(1).

提交回复
热议问题