Minimum Cost Good Caption - Problem

You're working on a social media platform that validates captions for posts. A good caption is one where every character appears in groups of at least 3 consecutive occurrences.

Examples:

  • "aaabbb" ✅ Good caption (3 a's, 3 b's)
  • "aaaaccc" ✅ Good caption (4 a's, 3 c's)
  • "aabbb" ❌ Bad caption (only 2 a's)
  • "ccccd" ❌ Bad caption (only 1 d)

You can modify any character by changing it to an adjacent alphabet character:

  • Change to previous character (e.g., 'b' → 'a'), unless it's 'a'
  • Change to next character (e.g., 'b' → 'c'), unless it's 'z'

Goal: Transform the caption into a good caption using the minimum number of operations. If multiple solutions exist, return the lexicographically smallest one. If impossible, return an empty string.

Input & Output

example_1.py — Basic Transformation
$ Input: "aabcc"
Output: "aaabbb"
💡 Note: Change the last 'c' to 'b' (cost 1) and the second-to-last 'c' to 'b' (cost 1). Total cost: 2. Result: "aaabbb" with groups of 3 a's and 3 b's.
example_2.py — Lexicographically Smallest
$ Input: "abc"
Output: "aaa"
💡 Note: Transform all characters to 'a': change 'b' to 'a' (cost 1) and 'c' to 'b' to 'a' (cost 2). Total cost: 3. "aaa" is lexicographically smaller than "bbb" or "ccc".
example_3.py — Impossible Case
$ Input: "az"
Output: ""
💡 Note: String length is 2, which is less than 3. Cannot form any groups of 3+ consecutive characters. Return empty string.

Constraints

  • 1 ≤ caption.length ≤ 1000
  • caption consists of only lowercase English letters
  • Each group must have at least 3 consecutive identical characters
  • Return lexicographically smallest result among minimum cost solutions

Visualization

Tap to expand
Minimum Cost Good Caption INPUT Original Caption: a a b c c 0 1 2 3 4 Constraints: - Each char must appear in groups of at least 3 - Can change to adjacent char Current Groups: "aa" (2) - Invalid "b" (1) - Invalid "cc" (2) - Invalid Length = 5 ALGORITHM (DP) 1 Define DP State dp[i][c] = min cost to make pos i valid, ending with char c 2 Try Each Group For each position, try groups of length 3, 4, 5 for each char 3 Calculate Cost Cost = sum of |s[j] - target| for each position in group 4 Track Best Path Keep lexicographically smallest among same-cost solutions Optimal Grouping: Option 1: aaa + bb (invalid) Option 2: aaa + bbb (cost=3) b-->a: 1, c-->b: 1, c-->b: 1 Total: 3 operations FINAL RESULT Good Caption: a a a b b "aaabbb" Transformations: Position 2: b --> a (1 op) Position 3: c --> b (1 op) Position 4: c --> b (1 op) Total Cost: 3 OK - Valid Groups: aaa, bbb Key Insight: The DP approach tries all possible groupings of length 3 or more at each position. For length 5, we can only split as 3+2 (invalid) or 5 (one group). Optimal is 3+3 but we have 5 chars, so we use one group of 3 ('aaa') and extend another group to 3 ('bbb'). Lexicographically smallest among min-cost solutions wins. TutorialsPoint - Minimum Cost Good Caption | Dynamic Programming Approach
Asked in
Google 23 Meta 18 Amazon 15 Microsoft 12
26.2K Views
Medium Frequency
~35 min Avg. Time
847 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