Largest Number After Mutating Substring - Problem

Imagine you're a digital magician working with a large number represented as a string. You have a magical change array that can transform any digit into another digit - think of it as a spell book where change[i] tells you what digit i should become.

Your mission: Choose exactly one contiguous substring from your number and apply the transformation to every digit in that substring. Your goal is to make the resulting number as large as possible.

For example, if your number is "132" and your change array maps [9,8,7,6,5,4,3,2,1,0], you could:

  • Transform the substring "13" → get "872"
  • Transform just "1" → get "932"
  • Or don't transform at all → keep "132"

The largest result would be "932"! Remember: you can choose not to transform if it would make the number smaller.

Input & Output

example_1.py — Basic Transformation
$ Input: num = "132", change = [9,8,7,6,5,4,3,2,1,0]
Output: "867"
💡 Note: We can transform the entire string "132" to "867" since each digit can be improved: 1→8, 3→6, 2→7. This gives us the maximum possible value.
example_2.py — Partial Transformation
$ Input: num = "021", change = [9,8,7,6,5,4,3,2,1,0]
Output: "934"
💡 Note: Transform "021" to "934". We change 0→9, 2→7, 1→8. Even though 2→7 makes it larger, we continue because it's beneficial, and 1→8 is also beneficial.
example_3.py — No Transformation
$ Input: num = "5", change = [1,4,2,3,0,5,6,7,8,9]
Output: "5"
💡 Note: The digit 5 would change to 5 (no improvement), so we don't transform anything and return the original number.

Constraints

  • 1 ≤ num.length ≤ 105
  • change.length == 10
  • 0 ≤ change[i] ≤ 9
  • num consists of only digits 0-9
  • You must choose exactly one substring (which can be empty - meaning no transformation)

Visualization

Tap to expand
🎭 The Magic Number Transformation🔮 Original Number: 132📜 Change Spell: [9,8,7,6,5,4,3,2,1,0]✨ Step 1: Find First Magic Opportunity1→ 8 ✨ (Improvement!)32🌟 Step 2: Continue the Magical TransformationTransformation Spell Active81→8✓ Good63→6✓ Good72→7✓ Good🎉 Final Result: 867Original 132 → Transformed 867Maximum possible improvement achieved!
Understanding the Visualization
1
Scan for Opportunity
Look from left to right for the first digit that can be improved
2
Start the Magic
Begin transformation at the first beneficial position
3
Continue the Spell
Keep transforming as long as digits improve or stay same
4
Stop Before Harm
Halt transformation before making any digit smaller
Key Takeaway
🎯 Key Insight: The greedy approach works because we want to maximize the leftmost digits first (highest impact), and once we start a transformation, we should continue it as long as it doesn't hurt the result.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
24.8K Views
Medium Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen