Optimized counter by preparing trie to remove an inner loop check
Now uses customized trie with O(3) method for incrementing a value