Algorithm for grouping anagram words

前端 未结 14 1549
悲&欢浪女
悲&欢浪女 2020-12-07 23:30

Given a set of words, we need to find the anagram words and display each category alone using the best algorithm.

input:

man car kile arc none like
<         


        
14条回答
  •  情书的邮戳
    2020-12-08 00:10

    Assign a unique prime number to the letters a-z

    Iterate your word array, creating a product of primes based on the letters in each word.
    Store that product in your word list, with the corresponding word.

    Sort the array, ascending by the product.

    Iterate the array, doing a control break at every product change.

提交回复
热议问题