Sort the Jumbled Numbers

Medium Topics Company Tags

Replace each decimal digit d of a number with mapping[d] to obtain its mapped value. Leading zeros in this new value do not affect its numeric value. Sort nums by these mapped values in nondecreasing order and return the original numbers in that order. When mapped values are equal, preserve the numbers' original relative order. Treat the number 0 as a single digit, so its mapped value is mapping[0].

Example 1:

Input: mapping = [1,0,2,3,4,5,6,7,8,9]
nums = [10,0,1,100]

Output: [1,10,0,100]

Explanation: The mapped values of 10, 0, 1, and 100 are 1, 1, 0, and 11. Put 1 first, keep 10 before 0 because their mapped values tie, and put 100 last.

Example 2:

Input: mapping = [9,8,7,6,5,4,3,2,1,0]
nums = [9,99,999,0]

Output: [9,99,999,0]

Explanation: The values 9, 99, and 999 all map to zero, so their original order is preserved. The original number 0 maps to 9 and comes last.

Example 3:

Input: mapping = [0,1,2,3,4,5,6,7,8,9]
nums = [12,3,12,0]

Output: [0,3,12,12]

Explanation: Every digit maps to itself, so this is ordinary numeric sorting. The repeated value 12 is retained twice.

Constraints:

  • mapping is a permutation of the digits from 0 through 9.
  • 1 <= nums.length <= 30000
  • 0 <= nums[i] < 1000000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.