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
sends with the'#'character - For each index
indices[i], the substring ofsstarting fromindices[i]and up to (but not including) the next'#'character is equal towords[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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code