#3441
Minimum Cost Good Caption
international master · 2195 · lc hard +32 · 20.7% accepted · 41 likes · top 2%
Description
You are given a string caption of length n. A good caption is one where every character appears in consecutive runs of at least 3.
For example:
- "aaabbb" and "aaaaccc" are good captions.
- "aabbb" and "ccccd" are not.
You may perform any number of operations, each time advancing or retreating one character at one index by one step in the alphabet ('a' cannot go back and 'z' cannot go forward).
Convert caption into a good caption using the minimum number of operations. Among all minimum-cost results, return the lexicographically smallest one. If it is impossible, return "".
Code
1
2
3