2191. 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.


  • 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.



Runtime: 633 ms, faster than 25%

Memory Usage: 8.2 MB, less than 100%

func sortJumbled(mapping []int, nums []int) []int {
    sort.Slice(nums, func(i, j int) bool { 
        return change(mapping, nums[i]) < change(mapping, nums[j]) 

    return nums

func change(mapping []int, num int) int {
    if num < 10 { return mapping[num] }

    res := 0
    mul := 1

    for num > 0 {
        res += mapping[num % 10] * mul
        mul *= 10
        num /= 10

    return res

Last updated