Short Encoding of Words

MediumStringstrie

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You want to store a list of words as one reference string ref that ends with #, together with an index for every word. Word words[i] is recovered by starting at its index in ref and reading up to (but not including) the next #. For example, words = ["time", "me", "bell"] can be stored as ref = "time#bell#" with indices [0, 2, 5], because "me" is the tail of "time". Return the length of the **shortest** possible reference string.

Examples

Input: words = ["time","me","bell"]
Output: 10
`"time#bell#"` works; `"me"` reuses the end of `"time"`.
Input: words = ["t"]
Output: 2
`"t#"`.

Constraints

  • `1 <= words.length <= 2000`
  • `1 <= words[i].length <= 7`
  • `words[i]` contains only lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More trie problems