FASTEST  C++  SOLUTION  |  Beats 100% 🔥💯
Please  UPVOTE \u2B06\uFE0F if you like this Solution !  \n\n---\n\n# Intuition\nTo solve this problem, we need to map each digit of the numbers in the input ve
# Please  UPVOTE \u2B06\uFE0F if you like this Solution !  \n\n---\n\n# Intuition\nTo solve this problem, we need to map each digit of the numbers in the input vector `nums` to a new digit based on the given `mapping` vector. After mapping, we should sort the numbers based on their mapped values. Additionally, if the mapped values of two numbers are the same, their relative order from the input vector should be preserved. This can be achieved by using a stable sort.\n\n---\n\n# Approach\n1. Create a vector `val` of pairs to store the mapped value and the original index of each number in `nums`.\n2. Iterate over each number in `nums`:\n   - For each digit of the number, map it to the new digit using the `mapping` vector.\n   - Construct the mapped value by considering the place value (units, tens, hundreds, etc.) of each digit.\n   - Store the mapped value and the original index in the `val` vector.\n3. Use `std::stable_sort` to sort the `val` vector based on the mapped values while maintaining the relative order of elements with equal mapped values.\n4. Create the result vector `res` by rearranging the numbers in `nums` according to the sorted order of their original indices stored in `val`.\n5. Return the result vector `res`.\n\n---\n\n# Complexity\n- Time complexity : $O(n * k + n*log(n) )$, where $n$ is the number of elements in `nums` and $k$ is the number of digits in the largest number. The $k$ factor comes from mapping each digit, and the $n*log(n)$ factor comes from the sort.\n- Space complexity : $O(n)$, where $n$ is the number of elements in `nums`, for storing the `val` vector.\n\n\n# Code\n``` c++ []\nclass Solution {\npublic:\n    vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {\n        int n = nums.size();\n        vector<pair<int, int>> val(n, pair<int, int>(0, 0));\n\n        for (int i = 0; i < n; ++i) { \n            int num = nums[i], j = 0, ans = 0;\n            if (num == 0) {\n                ans = mapping[num];\n            } else {\n                while (num > 0) {\n                    ans += pow(10, j) * mapping[num % 10];\n                    num /= 10;\n                    ++j;\n                }\n            }\n            val[i].first = ans;\n            val[i].second = i;\n        }\n        \n        sort(val.begin(), val.end());\n        vector<int> res(n);\n\n        for (int i = 0; i < n; ++i) { \n            res[i] = nums[val[i].second];\n        }\n\n        return res;\n    }\n};\n```
['Array', 'Sorting', 'C++']