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:
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.
Constraints:
1 <= num.length <= 105num consists of only digits 0-9.change.length == 100 <= change[d] <= 9When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a long number written on a paper strip and a special rulebook that lets you swap certain digits for bigger ones. The brute force approach involves testing every single possible section of the number to see what happens when you apply your upgrade rules to it.
Here's how the algorithm would work step-by-step:
def maximum_number(number_string, replacement_rules):
candidate_results = [number_string]
# Generate every continuous slice to test all possible substring mutation ranges
for start_index in range(len(number_string)):
for end_index in range(start_index + 1, len(number_string) + 1):
current_digits = list(number_string)
mutation_started = False
mutation_stopped = False
# Evaluate each digit within the chosen slice against the replacement rules
for digit_index in range(start_index, end_index):
original_digit_character = current_digits[digit_index]
original_digit_integer = int(original_digit_character)
replacement_digit_integer = replacement_rules[original_digit_integer]
replacement_digit_character = str(replacement_digit_integer)
if replacement_digit_integer > original_digit_integer:
current_digits[digit_index] = replacement_digit_character
mutation_started = True
elif replacement_digit_integer < original_digit_integer and mutation_started:
mutation_stopped = True
break
elif replacement_digit_integer == original_digit_integer and mutation_started:
pass
if mutation_started:
candidate_results.append("".join(current_digits))
if mutation_stopped and start_index == end_index - 1:
pass
# Compare all generated candidate strings lexicographically to find the absolute maximum value
return max(candidate_results)
To make the number as large as possible, we should scan the digits from left to right and replace a portion of them with larger mapped values if possible. The key is to start the replacement the exact moment a mapped digit becomes strictly larger than the original digit, and stop the moment a mapped digit becomes smaller.
Here's how the algorithm would work step-by-step:
def maximum_number(number_string: str, change_mapping: list) -> str:
digits_list = list(number_string)
is_mutating = False
# Iterate through digits to find the optimal window for maximizing value.
for index in range(len(digits_list)):
current_digit = int(digits_list[index])
mapped_digit = change_mapping[current_digit]
if mapped_digit > current_digit:
digits_list[index] = str(mapped_digit)
is_mutating = True
elif mapped_digit == current_digit and is_mutating:
continue
elif is_mutating:
# Stop mutation immediately when a mapped value becomes smaller.
break
return "".join(digits_list)| Case | How to Handle |
|---|---|
| No digits are beneficial to change | The algorithm simply does not mutate any substring and returns the original string unchanged. |
| Mutating the entire string provides the maximum value | The substring search continues to the end of the string and successfully mutates every digit. |
| A valid substring mutation starts and then immediately encounters a digit where change[d] < d | The greedy approach stops the mutation window immediately to prevent decreasing the number value. |
| Multiple disjoint substrings could theoretically be mutated | The problem statement restricts operations to at most one contiguous substring, so we only select the single best continuous segment. |
| The string consists of a single digit | The algorithm checks that single digit against its mapping and replaces it only if the mapped value is strictly greater. |
| Mapping replaces digits with equal values | The condition checks for strict inequality change[d] > d to start the mutation, avoiding unnecessary mutations that do not increase value. |
| Maximum length strings up to one hundred thousand characters | A single pass linear scan ensures the solution runs in optimal O(n) time complexity and passes within time limits. |
| String contains leading zeros | Leading zeros are processed normally as part of the string digits and mutated if their mapped value is larger. |