Basic Solution with easy explanation 🔥| C++✔️ | Java✔️ | Python✔️ |
Intuition\nSo , basically we need to transform each number in nums using the mapping array and then sort the transformed numbers while maintaining the original 
# Intuition\nSo , basically we need to transform each number in nums using the mapping array and then sort the transformed numbers `while maintaining the original relative order of numbers` that map to the same value. \n\n---\n\n\n# Example\n`Input: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]`\n- Mapping: [8,9,4,0,2,1,3,5,7,6]\nNumber: 991 -> Mapped: 669\nNumber: 338 -> Mapped: 007\nNumber: 38  -> Mapped: 07\n\n- Mapped Values: 007, 07, 669\nSorted Order (Maintaining Relative Order for Same Mapped Values): 338, 38, 991\n\n`How mapping happens ?`\n- **for 991**\n- mapping[9] = 6 (see 9th index of mapping array)\n- mapping[1] = 9\n- So mapping(991) = 669\n\n---\n\n# Approach\n1. `Mapping:` For each number in the nums array, convert it to its mapped value by replacing each digit according to the mapping array.\n2. `Sorting:` Sort the original numbers based on their mapped values. Use a stable sorting algorithm to ensure that numbers with the same mapped value maintain their relative order from the input.\n\n---\n\n\n# Complexity\n- Time complexity: `O(n log n)` for sorting the numbers based on their mapped values.\n\n- Space complexity:`O(n)`\n\n# Code\n\n```java []\nclass Solution {\n   \n    public int[] sortJumbled(int[] mapping, int[] nums) {\n        Integer[] numsArray = new Integer[nums.length];\n        for (int i = 0; i < nums.length; i++) {\n            numsArray[i] = nums[i];\n        }\n\n        // Sort numsArray based on mapped values\n        Arrays.sort(numsArray, new Comparator<Integer>() {\n            public int compare(Integer a, Integer b) {\n                int mappedA = mapValue(a, mapping);\n                int mappedB = mapValue(b, mapping);\n                return Integer.compare(mappedA, mappedB);\n            }\n        });\n\n        // Convert the sorted Integer array back to int array for the output\n        for (int i = 0; i < nums.length; i++) {\n            nums[i] = numsArray[i];\n        }\n\n        return nums;\n    }\n\n    // Method to convert a number based on the mapping\n    private int mapValue(int num, int[] mapping) {\n        StringBuilder mapped = new StringBuilder();\n        String numStr = String.valueOf(num);\n        for (char digit : numStr.toCharArray()) {\n            mapped.append(mapping[digit - \'0\']);\n        }\n        return Integer.parseInt(mapped.toString());\n    }\n\n}\n```\n```python []\nclass Solution(object):\n    def sortJumbled(self, mapping, nums):\n        def mapped_value(num):\n            return int("".join(str(mapping[int(digit)]) for digit in str(num)))\n    \n    # Sort nums by the transformed values, retaining the order for equal elements\n        nums.sort(key=mapped_value)\n        return nums\n```\n```C++ []\nclass Solution {\npublic:\n    vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {\n    int numCount = nums.size();\n    vector<pair<int, int>> mappedNumAndIndex(numCount);\n\n    for (int i = 0; i < numCount; ++i) {\n        int originalNum = nums[i];\n        string mappedNumStr = "";\n        \n        if (originalNum == 0) {\n            mappedNumStr = to_string(mapping[0]);\n        } else {\n            while (originalNum > 0) {\n                int digit = originalNum % 10;\n                mappedNumStr = to_string(mapping[digit]) + mappedNumStr;\n                originalNum /= 10;\n            }\n        }\n\n        int mappedNum = stoi(mappedNumStr);\n        mappedNumAndIndex[i] = {mappedNum, i};\n    }\n\n    sort(mappedNumAndIndex.begin(), mappedNumAndIndex.end());\n\n    vector<int> sortedNums;\n    for (const auto& pair : mappedNumAndIndex) {\n        sortedNums.push_back(nums[pair.second]);\n    }\n\n    return sortedNums;\n}\n};\n```\n\n
['Array', 'Sorting', 'Python', 'C++', 'Java']