News for hackers. Stuff that matters.
Re:Algorithms and Data Structures: Trie (Prefix tree) by kornish on Wednesday November 09, 2016 @09:52PM (#12917302)
It could be. Here's a python implementation:

  from collections import Counter
  
  freq_to_words = {}
  make_freq = lambda word: tuple(sorted(dict(Counter(word)).items()))
  
  with open('/usr/share/dict/words', 'r') as f:
      for line in f.read().split('\n'):
          freq = make_freq(line)
          if freq not in freq_to_words:
              freq_to_words[freq] = []
          freq_to_words[freq].append(line)
  
  print freq_to_words[make_freq('gas')] # ['gas', 'sag']
For time complexities, this requires O(n) preprocessing but can do O(1) lookups, since it's a hash structure.

There are no comments on this comment yet.