Short Encoding of Words - Problem

A valid encoding of an array of words is any reference string s and array of indices indices such that:

  • words.length == indices.length
  • The reference string s ends with the '#' character
  • For each index indices[i], the substring of s starting from indices[i] and up to (but not including) the next '#' character is equal to words[i]

Given an array of words, return the length of the shortest reference string s possible of any valid encoding of words.

Input & Output

Example 1 — Basic Suffix Removal
$ Input: words = ["time", "me", "bell"]
Output: 10
💡 Note: The word "me" is a suffix of "time", so we can encode it as part of "time". The encoding becomes "time#bell#" with length 10.
Example 2 — No Suffixes
$ Input: words = ["t"]
Output: 2
💡 Note: Only one word, so the encoding is "t#" with length 2.
Example 3 — Multiple Suffix Relationships
$ Input: words = ["time", "me", "e"]
Output: 5
💡 Note: Both "me" and "e" are suffixes of "time", so we only need to encode "time" as "time#" with length 5.

Constraints

  • 1 ≤ words.length ≤ 2000
  • 1 ≤ words[i].length ≤ 7
  • words[i] consists of only lowercase English letters

Visualization

Tap to expand
Short Encoding: Remove Suffix WordstimemebellKeepRemove (suffix)Keepsuffix of "time"Encoded: "time#bell#"Length = 4 + 1 + 4 + 1 = 10
Understanding the Visualization
1
Input
Array of words: ["time", "me", "bell"]
2
Process
Identify "me" as suffix of "time", remove it
3
Output
Encoding length: "time#bell#" = 10
Key Takeaway
🎯 Key Insight: Words that are suffixes of other words can be omitted from the encoding
Asked in
Google 15 Microsoft 8
23.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen