Form Old to New Number Mapping And Do Sorting | Java | C++ | [Video Solution]
Intuition, approach, and complexity discussed in video solution in detail\nhttps://youtu.be/XbbIoIcfcK8\n# Code\nJava\n\nclass Solution {\n    public int[] sort
# Intuition, approach, and complexity discussed in video solution in detail\nhttps://youtu.be/XbbIoIcfcK8\n# Code\nJava\n```\nclass Solution {\n    public int[] sortJumbled(int[] mapping, int[] nums) {\n        int numsLen = nums.length;\n        int newOldNumMp[][] = new int[numsLen][2];\n        for(int indx = 0; indx < numsLen; indx++){\n            int oldNum = nums[indx];\n            int newNum = getNewNum(oldNum, mapping);\n            newOldNumMp[indx] = new int[]{newNum, oldNum};\n        }\n        Arrays.sort(newOldNumMp, (a, b)->(a[0] - b[0]));\n        for(int indx = 0; indx < numsLen; indx++){\n            nums[indx] = newOldNumMp[indx][1];\n        }\n        return nums;\n    }\n    private int getNewNum(int oldNum, int mapping[]){\n         if(oldNum == 0)return mapping[0];\n         int newNum = 0, mul = 1;\n         while(oldNum > 0){\n            int dig = oldNum % 10;\n            newNum = newNum + mapping[dig] * mul;\n            mul *= 10;\n            oldNum /= 10;\n         }       \n         return newNum;\n    }\n}\n```\nC++\n```\nclass Solution {\npublic:\n    vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {\n        vector<pair<int, int>> mappedNums(nums.size());\n        for (int indx = 0; indx < nums.size(); ++indx) {\n            mappedNums[indx] = {getMappedNumber(nums[indx], mapping), nums[indx]};\n        }\n\n        stable_sort(mappedNums.begin(), mappedNums.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n            return a.first < b.first;\n        });\n\n        for (int indx = 0; indx < nums.size(); ++indx) {\n            nums[indx] = mappedNums[indx].second;\n        }\n\n        return nums;\n    }\n\nprivate:\n    int getMappedNumber(int oldNum, const vector<int>& mapping) {\n        int newNum = 0;\n        int mul = 1;\n        if (oldNum == 0) return mapping[0];\n        while (oldNum > 0) {\n            int digit = oldNum % 10;\n            newNum = mapping[digit] * mul + newNum;\n            mul *= 10;\n            oldNum /= 10;\n        }\n        return newNum;\n    }\n};\n```
['Array', 'Sorting', 'C++', 'Java']