Finding anagrams of a specific word in a list

73 Views Asked by At

So, the problem is:

Given an array of m words and 1 other word, find all anagrams of that word in the array and print them.

Do y’all have any faster algorithm?:)

I’ve succesfully coded this one, but it seems rather slow ( i’ve been using sorted() with a for loop + checking the length before). Found anagrams were added to a new array. Then printing the list of anagrams with a for loop again.

1

There are 1 best solutions below

1
Paweł Pietraszko On

I think that counting characters and comparing it will be faster but im not sure. Just check it ;)

defaultdict will be helpful.

from collections import defaultdict as dd

def char_counter(word: str)-> dict
    result = dd(int)
    for c in word:
        result[c]+=1
    return result