Ctrl K

Order by Scrambled Digits

Medium
The cipher code is a permutation of the digits 0 to 9: digit d is replaced with code[d]. To get the **code** of a number, replace every digit of its decimal form using the cipher and read the result as a number (leading zeros are dropped). You are given an array values. Sort it by the codes of its elements in ascending order and return the result. The answer holds the original numbers, not their codes. Numbers with equal codes keep their relative order from values.
Example 1
code=[3, 1, 4, 0, 5, 9, 2, 6, 8, 7], values=[40, 13, 7] → [7, 13, 40]

40 turns into 53 (4 → 5, 0 → 3), 13 into 10 (1 → 1, 3 → 0), and 7 into 6. By coded value: 7, 13, 40.

Example 2
code=[5, 0, 1, 2, 3, 4, 6, 7, 8, 9], values=[11, 1, 50] → [11, 1, 50]

11 turns into 00, which is 0, and 1 also turns into 0, so they keep their original order. 50 turns into 45 and goes last.

Example 3
code=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], values=[21, 3] → [3, 21]
Constraints
  • len(code) = 10, code is a permutation of the digits 0–9
  • 1 ≤ len(values) ≤ 3·10⁴
  • 0 ≤ values[i] < 10¹⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsZero-Sum Triples MediumTriple Sum Nearest the Target MediumFour Numbers to a Target MediumDistinct Orderings With Repeats MediumAnagram Buckets Medium
loading Python…
Input
code=[3, 1, 4, 0, 5, 9, 2, 6, 8, 7], values=[40, 13, 7]
Expected
[7, 13, 40]

Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.