Tap here to switch tabs
Problem
Submissions

Problem: Replace Question Marks in String to Minimize Its Value

Replace Question Marks in String to Minimize Its Value asks you to fill question marks so repeated characters cost as little as possible, then return the lexicographically smallest result.

Statement

You are given a string s consisting of lowercase English letters and the character '?'.

For any string, the cost of the character at index i is defined as the number of times the character s[i] appears before index i (that is, at indices j<ij < i). The total value of the string is the sum of the costs across all indices from 00 to s.length −1- 1.

Replace every '?' in s with a lowercase English letter such that the total value of the string is minimized. If there are multiple strings that achieve the minimum total value, return the lexicographically smallest one.

Constraints:

  • 1≤1 \leq s.length ≤105\leq 10^5

  • s[i] is either a lowercase English letter or '?'.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Replace Question Marks in String to Minimize Its Value

Replace Question Marks in String to Minimize Its Value asks you to fill question marks so repeated characters cost as little as possible, then return the lexicographically smallest result.

Statement

You are given a string s consisting of lowercase English letters and the character '?'.

For any string, the cost of the character at index i is defined as the number of times the character s[i] appears before index i (that is, at indices j<ij < i). The total value of the string is the sum of the costs across all indices from 00 to s.length −1- 1.

Replace every '?' in s with a lowercase English letter such that the total value of the string is minimized. If there are multiple strings that achieve the minimum total value, return the lexicographically smallest one.

Constraints:

  • 1≤1 \leq s.length ≤105\leq 10^5

  • s[i] is either a lowercase English letter or '?'.