{"pageProps":{"post":{"title":"820. Short Encoding of Words","excerpt":"Given an array of words, return the length of the shortest reference string s possible of any valid encoding of words.","featuredImage":{"url":"https://media.graphassets.com/osdqSQYqRsuoPm7N7jhn"},"author":{"name":"Md Jawad Noor Asif","bio":"A humble student of knowledge","photo":{"url":"https://media.graphassets.com/nky5NwkETTKtpNnikulF"}},"createdAt":"2022-06-20T06:13:02.175831+00:00","slug":"short-encoding-of-words","content":{"raw":{"children":[{"type":"heading-one","children":[{"text":"Problem:"}]},{"type":"paragraph","children":[{"text":"A "},{"bold":true,"text":"valid encoding"},{"text":" of an array of "},{"code":true,"text":"words"},{"text":" is any reference string "},{"code":true,"text":"s"},{"text":" and array of indices "},{"code":true,"text":"indices"},{"text":" such that:"}]},{"type":"bulleted-list","children":[{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"words.length == indices.length"}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"text":"The reference string "},{"code":true,"text":"s"},{"text":" ends with the "},{"code":true,"text":"'#'"},{"text":" character."}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"text":"For each index "},{"code":true,"text":"indices[i]"},{"text":", the "},{"bold":true,"text":"substring"},{"text":" of "},{"code":true,"text":"s"},{"text":" starting from "},{"code":true,"text":"indices[i]"},{"text":" and up to (but not including) the next "},{"code":true,"text":"'#'"},{"text":" character is equal to "},{"code":true,"text":"words[i]"},{"text":"."}]}]}]},{"type":"paragraph","children":[{"text":"Given an array of "},{"code":true,"text":"words"},{"text":", return "},{"text":"the ","italic":true},{"bold":true,"text":"length of the shortest reference string"},{"text":" ","italic":true},{"code":true,"text":"s"},{"text":" possible of any ","italic":true},{"bold":true,"text":"valid encoding"},{"text":" of ","italic":true},{"code":true,"text":"words"},{"text":".","italic":true}]},{"type":"paragraph","children":[{"text":" "}]},{"type":"paragraph","children":[{"bold":true,"text":"Example 1:"}]},{"type":"code-block","children":[{"bold":true,"text":"Input:"},{"text":" words = [\"time\", \"me\", \"bell\"]\n"},{"bold":true,"text":"Output:"},{"text":" 10\n"},{"bold":true,"text":"Explanation:"},{"text":" A valid encoding would be s = "},{"code":true,"text":"\"time#bell#\" and indices = [0, 2, 5"},{"text":"].\nwords[0] = \"time\", the substring of s starting from indices[0] = 0 to the next '#' is underlined in \""},{"text":"time","underline":true},{"text":"#bell#\"\nwords[1] = \"me\", the substring of s starting from indices[1] = 2 to the next '#' is underlined in \"ti"},{"text":"me","underline":true},{"text":"#bell#\"\nwords[2] = \"bell\", the substring of s starting from indices[2] = 5 to the next '#' is underlined in \"time#"},{"text":"bell","underline":true},{"text":"#\"\n"}]},{"type":"paragraph","children":[{"bold":true,"text":"Example 2:"}]},{"type":"code-block","children":[{"bold":true,"text":"Input:"},{"text":" words = [\"t\"]\n"},{"bold":true,"text":"Output:"},{"text":" 2\n"},{"bold":true,"text":"Explanation:"},{"text":" A valid encoding would be s = \"t#\" and indices = [0].\n"}]},{"type":"paragraph","children":[{"text":" "}]},{"type":"paragraph","children":[{"bold":true,"text":"Constraints:"}]},{"type":"bulleted-list","children":[{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"1 <= words.length <= 2000"}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"1 <= words[i].length <= 7"}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"words[i]"},{"text":" consists of only lowercase letters."}]}]}]},{"type":"paragraph","children":[{"text":""}]},{"type":"heading-one","children":[{"text":"Solution:"}]},{"type":"class","children":[{"type":"code-block","children":[{"text":"class Solution(object):\n def minimumLengthEncoding(self, words):\n words = list(set(words)) #remove duplicates\n #Trie is a nested dictionary with nodes created\n # when fetched entries are missing\n Trie = lambda: collections.defaultdict(Trie)\n trie = Trie()\n\n #reduce(..., S, trie) is trie[S[0]][S[1]][S[2]][...][S[S.length - 1]]\n nodes = [reduce(dict.__getitem__, word[::-1], trie)\n for word in words]\n\n #Add word to the answer if it's node has no neighbors\n return sum(len(word) + 1\n for i, word in enumerate(words)\n if len(nodes[i]) == 0)"}]}],"className":"python"},{"type":"paragraph","children":[{"text":""}]}]}},"categories":[{"name":"LeetCode","slug":"leetcode"},{"name":"Python","slug":"python"},{"name":"Trie","slug":"trie"}]}},"__N_SSG":true}