Class Solution
-
- All Implemented Interfaces:
public final class Solution3081 - Replace Question Marks in String to Minimize Its Value.
Medium
You are given a string
s.s[i]is either a lowercase English letter or'?'.For a string
thaving lengthmcontaining only lowercase English letters, we define the functioncost(i)for an indexias the number of characters equal tot[i]that appeared before it, i.e. in the range[0, i - 1].The value of
tis the sum ofcost(i)for all indicesi.For example, for the string
t = "aab":cost(0) = 0cost(1) = 1cost(2) = 0Hence, the value of
"aab"is0 + 1 + 0 = 1.
Your task is to replace all occurrences of
'?'inswith any lowercase English letter so that the value ofsis minimized.Return a string denoting the modified string with replaced occurrences of
'?'. If there are multiple strings resulting in the minimum value , return the lexicographically smallest one.Example 1:
Input: s = "???"
Output: "abc"
Explanation: In this example, we can replace the occurrences of
'?'to makesequal to"abc".For
"abc",cost(0) = 0,cost(1) = 0, andcost(2) = 0.The value of
"abc"is0.Some other modifications of
sthat have a value of0are"cba","abz", and,"hey".Among all of them, we choose the lexicographically smallest.
Example 2:
Input: s = "a?a?"
Output: "abac"
Explanation: In this example, the occurrences of
'?'can be replaced to makesequal to"abac".For
"abac",cost(0) = 0,cost(1) = 0,cost(2) = 1, andcost(3) = 0.The value of
"abac"is1.Constraints:
<code>1 <= s.length <= 10<sup>5</sup></code>
s[i]is either a lowercase English letter or'?'.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final StringminimizeStringValue(String s)-
-
Method Detail
-
minimizeStringValue
final String minimizeStringValue(String s)
-
-
-
-