Python | Mapping | Easy
see the Successfully Accepted Submission\n\n# Code\n\nclass Solution:\n    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n        def
[see the Successfully Accepted Submission](https://leetcode.com/problems/sort-the-jumbled-numbers/submissions/1331583876/?envType=daily-question&envId=2024-07-24)\n\n# Code\n```\nclass Solution:\n    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n        def map_number(num: int) -> int:\n            mapped_num_str = \'\'.join(str(mapping[int(digit)]) for digit in str(num))\n            return int(mapped_num_str)\n        \n        # Create a list of tuples where each tuple is (original_num, mapped_value)\n        mapped_pairs = [(num, map_number(num)) for num in nums]\n        \n        # Sort based on the mapped values, while maintaining original order for same mapped values\n        mapped_pairs.sort(key=lambda x: x[1])\n        \n        # Extract the sorted original numbers\n        sorted_nums = [num for num, mapped_value in mapped_pairs]\n        \n        return sorted_nums\n        \n```\n\n## Steps:\n\n1. **Mapping Digits**: We need to create a function to map each digit of a number according to the given mapping array.\n2. **Mapping Numbers**: For each number in the `nums` array, we map it to its corresponding value using the function from step 1.\n3. **Sorting by Mapped Values**: We sort the original numbers in the `nums` array based on their mapped values, maintaining the original relative order for numbers with the same mapped values.\n\n## Explanation\n\n### Mapping Function\nThe function `map_number` takes an integer `num` and returns its mapped value based on the `mapping` array. It converts each digit of `num` according to the `mapping` and combines these digits back into a new integer.\n\n### Mapping Numbers\nWe use a list comprehension to create a list of tuples `mapped_pairs`, where each tuple contains the original number and its mapped value.\n\n### Sorting\nWe sort the `mapped_pairs` list by the mapped values using the `sorted` function with a custom key. This ensures that the sort is stable and maintains the relative order of numbers with the same mapped value.\n\n### Result Extraction\nAfter sorting, we extract the original numbers from the `mapped_pairs` list to get the final sorted list.\n\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']