KatInterview

Compact RLE Coder: Decode a String and Re-encode Under Pattern Limit

Dynamic ProgrammingAuto-graded · 20 test cases
MediumAuto-gradedDynamic Programmingstringsdynamic-programmingcompression

Last updated

Tests string parsing for RLE decoding and interval DP to find the shortest valid re-encoding when patterns are capped at length K.

Background

A compressed string is built from a sequence of tokens joined end-to-end. Each token looks like:

<count><pattern>
  • count — a positive integer in base 10 (one or more digits, no leading +).
  • pattern — a non-empty string of lowercase letters a–z.

Because digits never appear in patterns and letters never appear in counts, token boundaries are always unambiguous. The token expands to pattern repeated count times.

Example: "2aab3ab" expands as "aab"+"aab"+"ab"+"ab"+"ab" → "aabaabababab".


Your Tasks

Part A — Decode

Given a valid encoded string encoded, return its fully expanded form.

Part B — Optimal Re-encode

Given a plain string s and a positive integer K, produce an encoded string that:

  1. Decodes back to exactly s.
  2. Uses only patterns whose length is at most K in every token.
  3. Has the shortest possible total character count among all such encodings.

If several shortest encodings exist, return any one of them.


Return Value

Return a list of two strings: [decoded_result, encoded_result].


Notes

  • Every token must follow the <count><pattern> format; bare (uncounted) literal substrings are not allowed.
  • A single character, or any substring, can always be emitted as count 1, so a valid encoding always exists.
  • count may be 10 or larger; your decoder must handle multi-digit counts correctly.
  • For encoding, greedily taking the longest repeating run is not always optimal — the non-linear cost of extra digits in large counts means you must consider every valid count, not just the maximum.

Constraints

  • 1 ≤ len(encoded) ≤ 1000 and encoded is a valid token sequence.
  • 1 ≤ len(s) ≤ 200.
  • 1 ≤ K ≤ len(s).
  • s and all patterns in encoded contain only lowercase letters a–z.
  • The decoded expansion of encoded fits in memory as a plain Python string.

Examples

Example 1

Input: rle_decode_and_encode('2aab3ab', 'aabaabababab', 3)

Output: ['aabaabababab', '2aab3ab']

Example 2

Input: rle_decode_and_encode('1a', 'a', 1)

Output: ['a', '1a']

Hints

Hint 1

For decoding, scan left to right: collect consecutive digit characters to form the count, then collect consecutive letter characters to form the pattern. Repeat until the string is exhausted — digit/letter alternation makes boundaries unambiguous.

Hint 2

For encoding, think of it as a shortest-path problem over positions in s. Define dp[i] as the minimum encoded length of s[i:]. At each position, try every pattern length p from 1 to K and every valid repeat count c (all counts where s[i:i+c*p] is exactly c copies of s[i:i+p]), then pick the combination that minimises len(str(c)) + p + dp[i+c*p].

Hint 3

Watch out for the digit-cost cliff: going from count 9 to 10 adds a character to the count field, so taking the maximum run is not always cheapest. You must evaluate every count from 1 up to the maximum, not just the largest one.