The Minimum Cost Good Caption problem defines a "good caption" as a string where every character belongs to a group of at least three identical consecutive characters (e.g., "aaabbb" is good, "aabbb" is not). You are given an initial string and the cost to change any character to another is the absolute difference of their alphabetical positions. You need to find the minimum cost to make the string "good" and the lexicographically smallest such string.
This is a high-difficulty Minimum Cost Good Caption interview question from TikTok and Fractal Analytics. It's a complex Dynamic Programming problem involving string manipulation and lexicographical constraints. It tests if a candidate can handle multi-faceted state transitions and meticulous back-tracking to reconstruct the optimal string.
The String Dynamic Programming interview pattern with state dp[i][char][count] or similar is used.
dp[i][last_char] stores the minimum cost to make the prefix of length i good, ending in last_char.i, you can either continue the current character's block or start a new block of a different character (only if the current block is already ).String "aba", costs 1 per shift.
|b-a| = 1), "bbb" (cost |a-b| + |a-b| = 2).When a problem asks for "minimum cost" AND "lexicographically smallest," you almost always need to store the choice you made at each step in the DP. This allows you to backtrack from the end to the start to build the result string.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Valid Palindrome III | Hard | Solve | |
| Distinct Subsequences | Hard | Solve | |
| Check if an Original String Exists Given Two Encoded Strings | Hard | Solve | |
| Count Different Palindromic Subsequences | Hard | Solve | |
| Count Palindromic Subsequences | Hard | Solve |