KatInterview

Shortest Nested Run-Length Encoding of a String

Dynamic ProgrammingAuto-graded · 24 test cases
MediumAuto-gradedDynamic Programmingdynamic-programmingstringsrecursion

Last updated

Find the shortest nested repeat encoding of a string with interval DP over splits and repeats, using a fixed tie-break order so the output is unique.

Nested Repeat Encoding

You are given a string text. You may shrink it by writing a block of consecutive identical copies compactly as a token.

Token format

A token has the form k{p}:

  • k is an integer with k >= 2, written in decimal.
  • p is a string, which may itself be an encoded string containing further tokens.
  • The token stands for the decoded form of p repeated k times in a row.

Tokens and plain characters can be placed next to each other freely, and tokens can be nested, for example 2{a3{b}}.

Task

Return the shortest encoding of text. If no encoding is strictly shorter than text, return text unchanged.

Making the answer unique

Several encodings can have the same minimum length (for instance, ten a characters could be written 10{a} or a9{a}). To get exactly one answer, the encoding of every substring t is defined by the following procedure, and your function must return the result for the whole text.

For a substring t of length L:

  1. If L == 1, the encoding is t itself.
  2. Otherwise start with the raw candidate t.
  3. Then examine further candidates in this order:
    1. Splits: for m = 1, 2, ..., L-1, the candidate is enc(t[:m]) + enc(t[m:]), where enc is the encoding of each part, defined by this same procedure.
    2. Repeats: for each unit length d = 1, 2, ..., L-1 where d divides L and t consists of t[:d] repeated L/d times, the candidate is "{L/d}{" + enc(t[:d]) + "}".
  4. A candidate replaces the current best only if it is strictly shorter. So among equal-length candidates, the earliest one in the order above (raw first) wins.

The encoding of t is the best candidate after all candidates have been examined.

For the empty string, return the empty string.

Constraints

  • 0 <= len(text) <= 150
  • text contains no digits and no { or } characters (so tokens are unambiguous).
  • Counts k may have several digits, and every digit counts toward the length of the encoding.

Examples

Example 1

Input: shortest_nested_encoding('aaa')

Output: 'aaa'

Example 2

Input: shortest_nested_encoding('ababab')

Output: '3{ab}'

Example 3

Input: shortest_nested_encoding('aabcaabcd')

Output: '2{aabc}d'

Example 4

Input: shortest_nested_encoding('aaaaaaaaaa')

Output: 'a9{a}'

Hints

Hint 1

Think of the answer for each substring as a subproblem: the best encoding of text[i..j] is built from the best encodings of smaller substrings.

Hint 2

For one substring there are three kinds of candidates: leave it raw, cut it into two parts and encode each part, or, if it is a block of identical copies of a shorter unit, wrap the unit's best encoding in a token.

Hint 3

Memoize on the (start, end) pair. Follow the candidate order and the strict-shorter rule from the statement exactly, since that decides ties such as a9{a} versus 10{a}.