Advertisement

Largest Number After Mutating Substring - LeetCode 1946 Solution

Largest Number After Mutating Substring - Complete Solution Guide

Largest Number After Mutating Substring is LeetCode problem 1946, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 si

Detailed Explanation

The problem asks us to find the largest possible number that can be obtained by mutating at most one substring of a given number `num`. We are given a `change` array that maps each digit from 0 to 9 to another digit. To mutate a substring, we replace each digit in the substring with the digit it maps to in the `change` array. If no mutation results in a larger number, we can return the original number. The input `num` is a string representing a large integer, and `change` is an array of integers representing the mapping of digits.

Solution Approach

The solution iterates through the input number string. For each digit, it checks if the changed value (using the `change` array) is greater than the original digit. If it is, it starts a mutation. During the mutation, it replaces the original digit with the changed one. It continues the mutation as long as the changed digit is greater than or equal to the original. Once the changed digit is less than the original, the mutation stops. If no mutation occurs, the original string is returned.

Step-by-Step Algorithm

  1. Step 1: Convert the input number string into a list/array of characters for easier manipulation.
  2. Step 2: Initialize a boolean variable `mutated` to `False` to track if a mutation has started.
  3. Step 3: Iterate through the list/array of characters representing the number.
  4. Step 4: For each digit, find its corresponding changed value using the `change` array.
  5. Step 5: If the changed value is greater than the original digit, start/continue the mutation by replacing the original digit with the changed value and set `mutated` to `True`.
  6. Step 6: If the changed value is equal to the original digit, continue the mutation by replacing the original digit with the changed value, but do not start a new mutation.
  7. Step 7: If the changed value is smaller than the original digit and `mutated` is `True`, stop the mutation.
  8. Step 8: If `mutated` is `True` and the changed value is smaller, break the loop. If `mutated` is `False` and the changed value is smaller, continue the iteration.
  9. Step 9: After the iteration, convert the list/array of characters back into a string and return it.

Key Insights

  • Insight 1: The problem can be solved using a greedy approach. We iterate through the number from left to right and start mutating the substring as soon as we find a digit that can be increased by applying the change array.
  • Insight 2: Once we start mutating, we continue as long as the mutated digit is greater than or equal to the original digit. As soon as the mutated digit becomes smaller, we stop the mutation.
  • Insight 3: There may be no profitable mutation, in which case we return the original string.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, String, Greedy.

Companies

Asked at: Infosys.