Move digit encoding to its method
Improve encoding logic: use unordered_map