Unlocking Number Mapping: A LeetCode Solution Like Never Before! 🧠
"Programming is like sex. One mistake and you have to support it for the rest of your life." \u2014 Michael Sinz\n\n\n\n\n# Intuition\nWe need to sort an array 
**"Programming is like sex. One mistake and you have to support it for the rest of your life." \u2014 Michael Sinz**\n\n![55nb8c.jpg](https://assets.leetcode.com/users/images/a0454e78-6bda-44b8-a37b-58d7727103dd_1721791038.0263846.jpeg)\n\n\n# Intuition\nWe need to sort an array of numbers based on their "mapped" values, which are derived from a custom mapping of digits. Each digit of a number is replaced by its mapped digit, and then the resulting number is used for sorting.\n\n# Approach\nMapping Digits \uD83D\uDD22:\n\n- Replace each digit of a number with its corresponding value from the mapping array.\nExample: If mapping[9] = 6, digit \'9\' becomes \'6\'.\n\nCompute Mapped Values \uD83E\uDDEE:\n\n- For each number in the nums array, compute its mapped value.\n- Maintain the original index to preserve relative order for numbers with the same mapped value (stable sorting).\n\nSort Based on Mapped Values \uD83D\uDCCA:\n\n- Use the mapped values to sort the original numbers.\n- Keep track of original indices to maintain stability.\n\nReturn Sorted Array \uD83C\uDFC1:\n\nAfter sorting, construct the result array using the original indices from the sorted mapped values.\n# Code\n```C++ []\nclass Solution {\npublic:\n    // Function to get the mapped value of a number based on the given mapping\n    int getMappedValue(vector<int>& mapping, int num) {\n        int mappedValue = 0;\n        int place = 1;\n        if (num == 0) {\n            return mapping[0];\n        }\n        while (num > 0) {\n            int digit = num % 10;\n            mappedValue += (place * mapping[digit]);\n            num /= 10;\n            place *= 10;\n        }\n        return mappedValue;\n    }\n\n    vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {\n        vector<int> result;\n        vector<pair<int, int>> mappedValuesWithIndex;\n\n        // Create a vector of pairs containing mapped values and their original indices\n        for (int i = 0; i < nums.size(); i++) {\n            int mappedValue = getMappedValue(mapping, nums[i]);\n            mappedValuesWithIndex.push_back({mappedValue, i});\n        }\n\n        sort(mappedValuesWithIndex.begin(), mappedValuesWithIndex.end());\n\n        // Construct the result vector based on the sorted mapped values and their original indices\n        for (const auto& pair : mappedValuesWithIndex) {\n            result.push_back(nums[pair.second]);\n        }\n\n        return result;\n    }\n};\n```\n```C []\nint getMappedValue(int mapping[], int num) {\n    int mappedValue = 0;\n    int place = 1;\n    if (num == 0) {\n        return mapping[0];\n    }\n    while (num > 0) {\n        int digit = num % 10;\n        mappedValue += (place * mapping[digit]);\n        num /= 10;\n        place *= 10;\n    }\n    return mappedValue;\n}\n\nint compare(const void *a, const void *b, void *mapping) {\n    int num1 = *(const int *)a;\n    int num2 = *(const int *)b;\n    int mappedValue1 = getMappedValue((int *)mapping, num1);\n    int mappedValue2 = getMappedValue((int *)mapping, num2);\n    return mappedValue1 - mappedValue2;\n}\n\nvoid sortJumbled(int mapping[], int nums[], int size) {\n    qsort_r(nums, size, sizeof(int), compare, mapping);\n}\n```\n```python []\ndef get_mapped_value(mapping, num):\n    mapped_value = 0\n    place = 1\n    if num == 0:\n        return mapping[0]\n    while num > 0:\n        digit = num % 10\n        mapped_value += place * mapping[digit]\n        num //= 10\n        place *= 10\n    return mapped_value\n\ndef sort_jumbled(mapping, nums):\n    mapped_values_with_index = [(get_mapped_value(mapping, num), index) for index, num in enumerate(nums)]\n    mapped_values_with_index.sort()\n    result = [nums[index] for _, index in mapped_values_with_index]\n    return result\n```\n```Java []\npublic class Solution {\n    public int getMappedValue(int[] mapping, int num) {\n        int mappedValue = 0;\n        int place = 1;\n        if (num == 0) {\n            return mapping[0];\n        }\n        while (num > 0) {\n            int digit = num % 10;\n            mappedValue += place * mapping[digit];\n            num /= 10;\n            place *= 10;\n        }\n        return mappedValue;\n    }\n\n    public int[] sortJumbled(int[] mapping, int[] nums) {\n        List<int[]> mappedValuesWithIndex = new ArrayList<>();\n        for (int i = 0; i < nums.length; i++) {\n            int mappedValue = getMappedValue(mapping, nums[i]);\n            mappedValuesWithIndex.add(new int[]{mappedValue, i});\n        }\n\n        Collections.sort(mappedValuesWithIndex, Comparator.comparingInt(a -> a[0]));\n\n        int[] result = new int[nums.length];\n        for (int i = 0; i < mappedValuesWithIndex.size(); i++) {\n            result[i] = nums[mappedValuesWithIndex.get(i)[1]];\n        }\n        return result;\n    }\n}\n\n```\n```Javascript []\nfunction getMappedValue(mapping, num) {\n    let mappedValue = 0;\n    let place = 1;\n    if (num === 0) {\n        return mapping[0];\n    }\n    while (num > 0) {\n        let digit = num % 10;\n        mappedValue += place * mapping[digit];\n        num = Math.floor(num / 10);\n        place *= 10;\n    }\n    return mappedValue;\n}\n\nfunction sortJumbled(mapping, nums) {\n    let mappedValuesWithIndex = nums.map((num, index) => [getMappedValue(mapping, num), index]);\n    mappedValuesWithIndex.sort((a, b) => a[0] - b[0]);\n    return mappedValuesWithIndex.map(pair => nums[pair[1]]);\n}\n```\n```C# []\npublic class Solution {\n    public int GetMappedValue(int[] mapping, int num) {\n        int mappedValue = 0;\n        int place = 1;\n        if (num == 0) {\n            return mapping[0];\n        }\n        while (num > 0) {\n            int digit = num % 10;\n            mappedValue += place * mapping[digit];\n            num /= 10;\n            place *= 10;\n        }\n        return mappedValue;\n    }\n\n    public int[] SortJumbled(int[] mapping, int[] nums) {\n        List<(int, int)> mappedValuesWithIndex = new List<(int, int)>();\n        for (int i = 0; i < nums.Length; i++) {\n            int mappedValue = GetMappedValue(mapping, nums[i]);\n            mappedValuesWithIndex.Add((mappedValue, i));\n        }\n\n        mappedValuesWithIndex.Sort((a, b) => a.Item1.CompareTo(b.Item1));\n\n        int[] result = new int[nums.Length];\n        for (int i = 0; i < mappedValuesWithIndex.Count; i++) {\n            result[i] = nums[mappedValuesWithIndex[i].Item2];\n        }\n\n        return result;\n    }\n}\n\n```\n\n\n
['C', 'Sorting', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']