Largest Number After Mutating Substring

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.

Example 1
Inputnum = "132", change = [9,8,5,0,3,6,4,2,6,8]
Output"832"
Replacing the substring "1" maps 1 to 8, so "132" becomes "832", which is the largest number that can be created.
Example 2
Inputnum = "021", change = [9,4,3,5,7,2,1,9,0,6]
Output"934"
Replacing the substring "021" maps 0 to 9, 2 to 3, and 1 to 4, so "021" becomes "934", which is the largest number that can be created.

Constraints

  • 1 <= num.length <= 10^5
  • num consists of only digits 0-9.
  • change.length == 10
  • 0 <= change[d] <= 9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate