Largest Number After Mutating Substring - Problem
You are given a string num, which represents a large integer. You are also given a 0-indexed integer array change of length 10 that maps each digit 0-9 to another digit. More formally, digit d maps to digit change[d].
You may choose to mutate a single substring of num. To mutate a substring, replace each digit num[i] with the digit it maps to in change (i.e. replace num[i] with change[num[i]]).
Return a string representing the largest possible integer after mutating (or choosing not to) a single substring of num.
A substring is a contiguous sequence of characters within the string.
Input & Output
Example 1 — Basic Transformation
$
Input:
num = "132", change = [9,8,5,0,2,6,7,1,4,3]
›
Output:
"855"
💡 Note:
Start at position 0: 1→8 (improvement), continue: 3→5 (improvement), 2→5 (improvement). Result: "855"
Example 2 — No Improvement Possible
$
Input:
num = "021", change = [9,8,5,0,2,6,7,1,4,3]
›
Output:
"021"
💡 Note:
0→9 would be improvement but we can't change leading 0s to make number larger lexicographically. Other digits: 2→5, 1→8 would help but starting with 0 limits us.
Example 3 — Partial Transformation
$
Input:
num = "5", change = [1,4,7,5,2,6,9,3,0,8]
›
Output:
"5"
💡 Note:
5→2 would make it smaller, so no transformation gives the best result
Constraints
- 1 ≤ num.length ≤ 105
- num consists of only digits 0-9
- change.length == 10
- 0 ≤ change[i] ≤ 9
Visualization
Tap to expand
Understanding the Visualization
1
Input
String num="132" and change array [9,8,5,0,2,6,7,1,4,3]
2
Transform
Apply optimal substring mutation: 1→8, 3→5, 2→5
3
Output
Largest possible number: "855"
Key Takeaway
🎯 Key Insight: Greedily transform from the first improvable digit, continuing while beneficial
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code