buildTrie and codeMap functions