- I think we can do the following:
- Convert each number in
nums
to a string - Then replace the characters:
''.join([str(mapping[int(i)]) for i in str(n)])
- After converting each number in
nums
, sort the values by the given array
- Convert each number in
- We could also solve this as a 1-liner:
return [m[0] for m in sorted(zip(nums, [int(''.join([str(mapping[int(i)]) for i in str(n)])) for n in nums]), key=lambda x: x[1])]
class Solution:
def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
return [m[0] for m in sorted(zip(nums, [int(''.join([str(mapping[int(i)]) for i in str(n)])) for n in nums]), key=lambda x: x[1])]
- Given test cases pass
- I can't think of any critical edge cases; submitting...
Solved!