All Posts

Leetcode 3081: Replace Question Marks in String to Minimize Its Value

You are given a string s consisting of lowercase English letters and question marks (?). Your task is to replace all occurrences of ? with any lowercase English letter in such a way that the total cost of the resulting string is minimized. The cost of a string is the sum of how many times each character has appeared before its current position. If there are multiple solutions with the same minimal cost, return the lexicographically smallest one.