Fill Wildcards to Minimize Cost

Asked at Amazon · Coding Questions · Difficulty: Medium

In a lowercase string `data`, `'?'` marks missing characters. The cost at index `i` is the number of earlier indices with the same character; the total cost is the sum. Replace every `'?'` to minimize the total cost, and among minimal-cost fillings return the lexicographically smallest string.

Loading the problem…