We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Sort the Jumbled Numbers

Number: 1333

Difficulty: Medium

Paid? No

Companies: Amazon, Google, Goldman Sachs


Problem Description

Given a mapping of digits (0-9) and an array of numbers, each digit of every number is replaced by its mapped counterpart. The resulting mapped value (interpreted as an integer that ignores any leading zeros) is used as a sorting key. The task is to sort the original numbers based on these mapped values in non-decreasing order while keeping the relative order of numbers with equal mapped values.


Key Insights

  • Create a helper function that converts an integer by mapping each of its digits using the provided mapping.
  • Convert the resulting mapped string into an integer to ignore any leading zeros.
  • Use a stable sorting algorithm so that numbers with the same mapped value maintain their relative order.
  • The transformation process adds only a constant factor since each number has at most 10 digits (given the constraint of numbers up to 10^9).

Space and Time Complexity

Time Complexity: O(n * d + n log n), where n is the number of elements in nums and d is the number of digits (at most 10).
Space Complexity: O(n), for storing the mapped values during the sort.


Solution

We first define a helper function that takes a number and returns its mapped value by iterating through its digits. For each digit, we look up its mapped counterpart and join them to form a new string, which is then converted to an integer (this conversion naturally removes any leading zeros). We then sort the original array of numbers using these mapped values as sorting keys. The sort must be stable, so that if two numbers have the same mapped value, their order relative to each other remains unchanged.

Data structures used include:

  • A string to build the mapped representation for each number.
  • An integer for the converted mapped value.

The algorithmic approach involves:

  1. Mapping each number to its transformed value.
  2. Performing a stable sort based on these transformed values.
  3. Returning the original numbers arranged in sorted order based on their mapped values.

Code Solutions

# Python implementation with line-by-line comments
def sortJumbled(mapping, nums):
    # Helper function to map a number using the provided digit mapping
    def getMappedValue(num):
        # Convert the number into its string representation
        num_str = str(num)
        # For each digit, find its mapped value and join them into a new string
        mapped_str = "".join(str(mapping[int(digit)]) for digit in num_str)
        # Convert the mapped string to an integer to remove any leading zeros and return it
        return int(mapped_str)
    
    # Sort the nums array. Python's sort is stable by default.
    sorted_nums = sorted(nums, key=getMappedValue)
    return sorted_nums

# Example usage:
mapping = [8,9,4,0,2,1,3,5,7,6]
nums = [991,338,38]
print(sortJumbled(mapping, nums))  # Output: [338,38,991]
← Back to All Questions