finding if two words are anagrams of each other

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

    1. Create a Hashmap where key - letter and value - frequencey of letter,
    2. for first string populate the hashmap (O(n))
    3. for second string decrement count and remove element from hashmap O(n)
    4. if hashmap is empty, the string is anagram otherwise not.

提交回复
热议问题