Mid/SeniorArraySorting

Sort the Jumbled Numbers

You are given a 0-indexed integer array mapping which represents the mapping rule of a shuffled decimal system. mapping[i] = j means digit i should be mapped to digit j in this system.

The mapped value of an integer is the new integer obtained by replacing each occurrence of digit i in the integer with mapping[i] for all 0 <= i <= 9.

You are also given another integer array nums. Return the array nums sorted in non-decreasing order based on the mapped values of its elements.

Notes:

  • Elements with the same mapped values should appear in the same relative order as in the input.
  • The elements of nums should only be sorted based on their mapped values and not be replaced by them.
Example 1
Inputmapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
Output[338,38,991]
The mapped values are 669 for 991, 7 for 338, and 7 for 38; since 338 and 38 tie, they keep their original relative order.
Example 2
Inputmapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]
Output[123,456,789]
789 maps to 789, 456 maps to 456, and 123 maps to 123, so sorting by mapped values gives [123,456,789].

Constraints

  • mapping.length == 10
  • 0 <= mapping[i] <= 9
  • All the values of mapping[i] are unique.
  • 1 <= nums.length <= 3 * 10^4
  • 0 <= nums[i] < 10^9

Asked at 4 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