If the prefixes of the words are repeatable, the trie tree will be most memory-efficient solution, unfortunately still O(N) pessimistically. You'll need to enrich the standard trie-tree class with additional information - words counters.
If you're looking for pessimistically optimal solution, multimap is a better solution:
O(1) insert time (not in trie tree if you have alphabet with many letters)
O(N) memory and running time
Still, you'll need to sort the words within the same occurrence count bucket, if there're many words with the same occurrence number, sorting becomes the dominant operation, and trie-tree approach is the same as multimap approach.