We need to sort numbers based on their "mapped" values, where each digit is replaced according to a mapping array. By converting each number to a string, we can easily iterate through its digits and build the mapped value. We store each mapped value along with the original index to preserve relative ordering for equal mapped values, then sort by the mapped values.
mapped value by iterating through each character, looking up its mapped digit, and constructing the new number.mapped value, original index) for each number.mapped value. Since the sort is stable, equal mapped values will maintain their original relative order.class Solution:
def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
pairs = []
for i, n in enumerate(nums):
n = str(n)
mapped_n = 0
for c in n:
mapped_n *= 10
mapped_n += mapping[int(c)]
pairs.append((mapped_n, i))
pairs.sort()
return [nums[p[1]] for p in pairs]Instead of converting numbers to strings, we can extract digits directly using arithmetic operations. We repeatedly take the last digit using modulo, map it, and accumulate the mapped value by multiplying by the appropriate power of 10. This avoids the overhead of string conversion while achieving the same result.
mapped value of 0 and a base multiplier of 1.0 by directly using the mapped value of digit 0.10, map it, multiply by the current base, and add to the mapped value. Then divide the number by 10 and multiply the base by 10.mapped value, original index).mapped value and construct the result using the stored indices.class Solution:
def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
pairs = []
for i, n in enumerate(nums):
mapped_n = 0
base = 1
if n == 0:
mapped_n = mapping[0]
else:
while n > 0:
digit = n % 10
n //= 10
mapped_n += base * mapping[digit]
base *= 10
pairs.append((mapped_n, i))
pairs.sort()
return [nums[p[1]] for p in pairs]When using arithmetic to extract digits, the number zero requires special handling. The while loop while (n > 0) never executes for zero, leaving the mapped value uninitialized. You must explicitly handle n == 0 by directly using mapping[0].
The problem requires that elements with equal mapped values maintain their original relative order. Using an unstable sort without tracking original indices will produce incorrect results when multiple numbers map to the same value.
For very large numbers, the mapped value can potentially overflow. While this is less common in languages with arbitrary precision integers like Python, in languages like Java or C++ you should be aware of the maximum input constraints to ensure the mapped value fits within the integer type.