Encode String with Shortest Length Problem
Description
LeetCode Problem 471.
Given a string s, encode the string such that its encoded length is the shortest.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. k should be a positive integer.
If an encoding process does not make the string shorter, then do not encode it. If there are several solutions, return any of them.
Example 1:
Input: s = "aaa"
Output: "aaa"
Explanation: There is no way to encode it such that it is shorter than the input string, so we do not encode it.
Example 2:
Input: s = "aaaaa"
Output: "5[a]"
Explanation: "5[a]" is shorter than "aaaaa" by 1 character.
Example 3:
Input: s = "aaaaaaaaaa"
Output: "10[a]"
Explanation: "a9[a]" or "9[a]a" are also valid solutions, both of them have the same length = 5, which is the same as "10[a]".
Constraints:
1 <= s.length <= 150sconsists of only lowercase English letters.
Solution
Approach: Interval DP
Let best[i][j] be a shortest encoding of the substring s[i..j]. A shortest encoding of a piece of the string has one of three forms:
- the piece itself, unencoded;
- the whole piece written as
k[x], where the piece iskcopies of some stringpandxis the shortest encoding ofp; - two parts written side by side,
best[i][m] + best[m + 1][j]for some split pointm.
Take the shortest of all of them. Both forms 2 and 3 only use shorter substrings, so filling the table with i from right to left and j from left to right has every value ready when it is needed. The answer is best[0][n - 1].
Finding the repetition. For form 2 we need the shortest period of t = s[i..j]: the shortest prefix that, repeated, gives t. A neat trick finds it. Search for t inside t + t, starting from index 1. The first index p where it appears is the shortest period. If p < len(t), then t is len(t) / p copies of t[0..p). Otherwise t does not repeat. Only the shortest period is needed: any other period is a multiple of it, so its block is itself made of copies of the shortest one, and the encoding inside the brackets is never shorter.
For example, t = "abbbabbbc" + "abbbabbbc" has period 9, so it can be written as 2[...] around the best encoding of "abbbabbbc". That one is in turn 2[abbb] followed by c, which gives 2[2[abbb]c].
Pieces shorter than 5 characters are left as they are: k[x] takes at least 4 characters, so it can never be strictly shorter. When two forms tie, keep the one found first, since any shortest answer is accepted.
Complexity: O(n3) time for the length comparisons (n2 pieces, each with up to n split points), plus the cost of building the chosen strings, each at most n long. O(n3) space for the stored strings, with n ≤ 150.
class Solution:
def encode(self, s: str) -> str:
n = len(s)
best = [[""] * n for _ in range(n)] # best[i][j] = a shortest encoding of s[i..j]
for i in range(n - 1, -1, -1):
for j in range(i, n):
t = s[i : j + 1]
best[i][j] = t
if len(t) < 5:
continue # "k[x]" takes at least 4 characters, so it cannot help
# The whole piece as k copies of its shortest period p: t occurs in t + t at index p.
p = (t + t).find(t, 1)
if p < len(t):
repeated = str(len(t) // p) + "[" + best[i][i + p - 1] + "]"
if len(repeated) < len(best[i][j]):
best[i][j] = repeated
# Or two parts encoded separately.
for k in range(i, j):
joined = best[i][k] + best[k + 1][j]
if len(joined) < len(best[i][j]):
best[i][j] = joined
return best[0][n - 1]