You are given a string caption of length n. A good caption is a string where every character appears in groups of at least 3 consecutive occurrences.
For example:
"aaabbb" and "aaaaccc" are good captions.
"aabbb" and "ccccd" are not good captions.
You can perform the following operation any number of times:
Choose an index i (where 0 <= i < n) and change the character at that index to either:
The character immediately before it in the alphabet (if caption[i] != 'a').
The character immediately after it in the alphabet (if caption[i] != 'z').
Your task is to convert the given caption into a good caption using the
minimum number of operations, and return it. If there are multiple possible good captions, return the lexicographically smallest one among them. If it is impossible to create a good caption, return an empty string
"".
Input: caption ="cdcd"Output: "cccc"Explanation:
It can be shown that the given caption cannot be transformed into a good
caption with fewer than 2 operations. The possible good captions that can be
created using exactly 2 operations are:*`"dddd"`: Change `caption[0]` and `caption[2]` to their next character `'d'`.*`"cccc"`: Change `caption[1]` and `caption[3]` to their previous character `'c'`.Since `"cccc"`is lexicographically smaller than `"dddd"`,return`"cccc"`.
Input: caption ="aca"Output: "aaa"Explanation:
It can be proven that the given caption requires at least 2 operations to be
transformed into a good caption. The only good caption that can be obtained
with exactly 2 operations is as follows:* Operation 1: Change `caption[1]` to `'b'`.`caption = "aba"`.* Operation 2: Change `caption[1]` to `'a'`.`caption = "aaa"`.Thus,return`"aaa"`.
Input: caption ="bc"Output: ""Explanation:
It can be shown that the given caption cannot be converted to a good caption
by using any number of operations.
To form a good caption, every character must appear in groups of at least 3 consecutive occurrences. We can use dynamic programming to try all possible groupings and character changes, keeping track of the minimum cost and lexicographically smallest result.
defminimum_cost_good_caption(caption: str) -> str:
n = len(caption)
dp = [[[float('inf')] *4for _ in range(26)] for _ in range(n +1)]
res = [[[''] *4for _ in range(26)] for _ in range(n +1)]
for c in range(26):
dp[0][c][0] =0for i in range(n):
for c in range(26):
for cnt in range(4):
if dp[i][c][cnt] == float('inf'):
continuefor nc in range(26):
cost = dp[i][c][cnt] + (caption[i] != chr(ord('a') + nc))
next_str = res[i][c][cnt] + chr(ord('a') + nc)
ncnt = cnt +1if nc == c else1 ncnt = min(ncnt, 3)
if cnt >=3or i ==0or nc == c:
if cost < dp[i +1][nc][ncnt] or (cost == dp[i +1][nc][ncnt] and next_str < res[i +1][nc][ncnt]):
dp[i +1][nc][ncnt] = cost
res[i +1][nc][ncnt] = next_str
min_cost = float('inf')
ans =''for c in range(26):
if dp[n][c][3] < min_cost or (dp[n][c][3] == min_cost and res[n][c][3] < ans):
min_cost = dp[n][c][3]
ans = res[n][c][3]
return''if min_cost == float('inf') else ans
1
// Skipped for brevity due to high memory usage in Go for this DP.
1
// Skipped for brevity due to high memory usage in Rust for this DP.
1
// Skipped for brevity due to high memory usage in TypeScript for this DP.