Encode String with Shortest Length - Problem
String Compression Challenge: Given a string s, your task is to encode it using a special compression rule to achieve the shortest possible length.

The encoding rule follows the pattern: k[encoded_string], where:
k is a positive integer representing repetition count
encoded_string is the substring being repeated exactly k times
• The brackets indicate the repeated section

Important: Only encode if it makes the string shorter! If encoding doesn't reduce length, keep the original substring.

Example: "aaabcbc""3[a]2[bc]" (length reduced from 7 to 8, so we'd actually return "aaabcbc")
Better example: "abababab""4[ab]" (length reduced from 8 to 5)

Input & Output

example_1.py — Basic Pattern
$ Input: s = "aaa"
Output: "aaa"
💡 Note: Since '3[a]' has the same length (4) as 'aaa' (3), we don't encode it because encoding doesn't make it shorter.
example_2.py — Multiple Patterns
$ Input: s = "aaaabaaaab"
Output: "2[2[a]b]"
💡 Note: We can encode 'aaaa' as '4[a]' but that's longer (4 chars vs 4 chars), so we keep 'aaaa'. However, 'aaaab' can become '4[a]b' (6 chars vs 5 chars - no improvement). But 'aaaabaaaab' can become '2[aaaab]' (9 chars vs 10 chars) or even better '2[4[a]b]' (8 chars) - but that's not shorter than original. The actual optimal is '2[2[a]ab]' = 8 chars vs 10 original.
example_3.py — No Encoding Needed
$ Input: s = "abcd"
Output: "abcd"
💡 Note: No repeating patterns exist, so the original string is already the shortest representation.

Constraints

  • 1 ≤ s.length ≤ 150
  • s consists of only lowercase English letters
  • Encoding is only applied if it reduces the string length
  • k should be a positive integer (k ≥ 2 for meaningful encoding)

Visualization

Tap to expand
String Encoding Process"abcabcabcabc"FindPatternabc×4CheckLength12→6ApplyEncoding4[abc]OptimizeResultBest: 6Dynamic Programming Approach1. Build memoization table for all substrings2. Try all possible splits: left + right combinations3. Check repeating patterns: k[encoded_pattern]4. Choose minimum length encoding for each substring5. Reuse computed results to avoid redundant work
Understanding the Visualization
1
Identify Patterns
Scan the string to find repeating subsequences like 'abab' or 'aaaa'
2
Calculate Savings
Check if encoding 'abab' as '2[ab]' actually saves space (4 chars vs 5 chars - saves 1)
3
Nested Optimization
Apply encoding recursively - '2[abc]' where 'abc' might also be encodable
4
Dynamic Building
Build optimal solutions for larger strings using optimal solutions of smaller parts
Key Takeaway
🎯 Key Insight: Dynamic programming with memoization transforms an exponential-time problem into O(n³) by reusing optimal solutions for substrings, making string encoding efficient and scalable.
Asked in
Google 42 Amazon 38 Meta 24 Microsoft 31
28.4K Views
Medium-High Frequency
~25 min Avg. Time
856 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