find number of anagrams for one array of string in respect to another array of string
Solution 1:
You could use Python dict
ionaries to speed things up:
dict_sorted = {}
for s in dictionary: # linear in terms of the size of `dictionary`
sorted_s = sorted(s.lower())
dict_sorted[sorted_s] = dict_sorted.get(sorted_s, 0) + 1
anagrams = []
for s in query: # linear in terms of the size of `query`
sorted_s = sorted(s.lower())
anagrams.append(dict_sorted.get(sorted_s, 0))
Using collections.Counter
to shorten things:
from collections import Counter
dict_sorted = Counter([sorted(s.lower()) for s in dictionary])
anagrams = [ dict_sorted.get(sorted(s.lower()), 0) for s in query ]