pattern made of lowercase English letters and ? marks. Every ? has to be replaced with some lowercase English letter.
The **cost** of the finished string works like this: walk through it from left to right and, for every letter, add how many times that same letter has already appeared to its left. For example, "abab" costs 0 + 0 + 1 + 1 = 2.
Replace all the ? marks so that the cost is as small as possible. If several strings reach that minimum, return the lexicographically smallest one.pattern='b??b' → 'bacb'
The two bs are already there, so one repeated pair is unavoidable. The cheapest choice for the question marks is letters not used yet, a and c. That gives "bacb" with cost 1, and it is the alphabetically smallest of all cost-1 options.
pattern='?zz?' → 'azzb'
Letters a and b are still unused. The smaller one, a, goes to the earlier position, because "azzb" is smaller than "bzza".
pattern='kite' → 'kite'
There are no question marks, so the string comes back unchanged.
len(pattern) ≤ 10⁵pattern[i] is a lowercase English letter or ?pattern='b??b'
'bacb'
Run checks the first 3 tests in your browser. Submit checks all 41 tests on the server, hidden ones included.