#1946
Largest Number After Mutating Substring
expert · 1090 · lc medium +32 · verified · 37.9% accepted · 237 likes · top 16%
Description
You are given a string num representing a large integer and a 0-indexed integer array change where change[d] is the new digit for digit d. You may mutate at most one contiguous substring of num by replacing each digit d with change[d].
Return the string representation of the largest possible integer obtainable (including the option to make no mutation).
Example 1:
Input: num = "132", change = [9,8,5,0,3,6,4,2,6,8]
Output: "832"
Explanation: Replace the substring "1":
- 1 maps to change[1] = 8.
Thus, "132" becomes "832".
"832" is the largest number that can be created, so return it.
Example 2:
Input: num = "021", change = [9,4,3,5,7,2,1,9,0,6]
Output: "934"
Explanation: Replace the substring "021":
- 0 maps to change[0] = 9.
- 2 maps to change[2] = 3.
- 1 maps to change[1] = 4.
Thus, "021" becomes "934".
"934" is the largest number that can be created, so return it.
Example 3:
Input: num = "5", change = [1,4,7,5,3,2,5,6,9,4]
Output: "5"
Explanation: "5" is already the largest number that can be created, so return it.
Code
1
2
3