✅💯🔥Explanations No One Will Give You🎓🧠2 Detailed Approaches🎯🔥Extremely Simple And Effective🔥
\n        \n            \n                Shoot for the moon. Even if you miss, you\'ll land among the stars.\n                    \n        \n        \n       
<blockquote>\n        <p>\n            <b>\n                Shoot for the moon. Even if you miss, you\'ll land among the stars.\n            </b>        \n        </p>\n        <p>\n            --- Norman Vincent Peale ---\n        </p>\n</blockquote>\n\n# \uD83D\uDC51Problem Explanation and Understanding:\n\n## \uD83C\uDFAF Problem Description\nYou are given an integer array `nums` and integer array `mapping`. You know that digit `i` is mapped to the digit `mapping[i]`. You want to sort `nums` based on what numbers you will get if you perform mapping on every its number (but you mustn\'t change the numbers in the output themselves)\n\n## \uD83D\uDCE5\u2935\uFE0F Input:\n- Integer array `nums`      | length <= 3 * 10^4 ; 0 <= nums[i] < 10^9\n- Integer array `mapping`   | length = 10 and every value is unique\n\n## \uD83D\uDCE4\u2934\uFE0F Output:\nSorted array based on mapping\n\n---\n\n# 1\uFE0F\u20E3\uD83C\uDFC6 Approach 1: Data Type Conversion\n\n# \uD83E\uDD14 Intuition\n- So, as it turns out, it\u2019s impossible to solve this problem without \u201Ctranslating\u201D each number (or at least I haven\u2019t found such a way, would be grateful if you share one if you found)\n- Thus, the task becomes quite easy (you just need "translate" every number and map to it the original), although the effectiveness of the solutions remains in question (converting every number digit by digit even with second approach seems very unefficient and probably would fail with really big number).\n- Okay, this is quite uncomfortable to change digits in the number as integer (though possible, check next approach) so let\'s convert every number to the str (or array in Python, for example) and then back to integer\n\n# \uD83D\uDC69\uD83C\uDFFB\u200D\uD83D\uDCBB Coding \n- Inside `sortJumbled`, define a helper function `translate_integer`:\n  - Convert the integer `num` to a array of its digits as strings.\n  - Replace each digit with the corresponding value from the `mapping` array.\n  - Join the modified digits back into a single string and convert it to an integer.\n- Create an empty hashmap `number_mapping`.\n- Iterate over each number in `nums`:\n  - Use the `translate_integer` function to convert the number.\n  - Store the result in `number_mapping` with the original number as the key.\n- Sort the `nums` array using a lambda function that sorts based on the translated values from `number_mapping`.\n- Return the sorted `nums` array.\n\n# \uD83D\uDCD7 Complexity Analysis\n- \u23F0 Time complexity: O(n * log n), since we use built-in sort functions (usually n * log n) and also perform many strings/array manipulations\n- \uD83E\uDDFA Space complexity: O(n), since sorts algorithms usually take extra space (O(n) of O(log n) depending on language) and we use `number_mapping` with size n -> O(n) anyway \n\n# \uD83D\uDCBB Code\n``` python []\nclass Solution:\n    def sortJumbled(self, mapping: list[int], nums: list[int]) -> list[int]:\n        def translate_integer(num: int) -> int:\n            digits: list[str] = list(str(num))\n            for i in range(len(digits)):\n                digits[i] = str(mapping[int(digits[i])])\n            return int("".join(digits))\n\n        number_mapping: dict[int, int] = {}\n        for num in nums:\n            number_mapping[num] = translate_integer(num)\n        nums.sort(key=lambda val: number_mapping[val])\n\n        return nums\n```\n``` C++ []\nclass Solution {\npublic:\n    vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {\n        auto translate_integer = [&](int num) -> int {\n            string digits = to_string(num);\n            for (char& digit : digits) {\n                digit = \'0\' + mapping[digit - \'0\'];\n            }\n            return stoi(digits);\n        };\n\n        unordered_map<int, int> number_mapping;\n        for (int num : nums) {\n            if (number_mapping.find(num) == number_mapping.end()) {\n                number_mapping[num] = translate_integer(num);\n            }\n        }\n\n        sort(nums.begin(), nums.end(), [&](int a, int b) {\n            return number_mapping[a] < number_mapping[b];\n        });\n\n        return nums;\n    }\n};\n```\n``` JavaScript []\nvar sortJumbled = function(mapping, nums) {\n    const translateInteger = (num) => {\n        let digits = num.toString().split(\'\');\n        for (let i = 0; i < digits.length; i++) {\n            digits[i] = mapping[digits[i]];\n        }\n        return parseInt(digits.join(\'\'));\n    };\n\n    const numberMapping = {};\n    for (let num of nums) {\n        if (!(num in numberMapping)) {\n            numberMapping[num] = translateInteger(num);\n        }\n    }\n\n    nums.sort((a, b) => numberMapping[a] - numberMapping[b]);\n\n    return nums;\n};\n```\n``` Java []\nclass Solution {\n    public int[] sortJumbled(int[] mapping, int[] nums) {\n        Map<Integer, Integer> numberMapping = new HashMap<>();\n\n        for (int num : nums) {\n            if (!numberMapping.containsKey(num)) {\n                numberMapping.put(num, translateInteger(num, mapping));\n            }\n        }\n\n        Integer[] numsBoxed = Arrays.stream(nums).boxed().toArray(Integer[]::new);\n        Arrays.sort(numsBoxed, (a, b) -> Integer.compare(numberMapping.get(a), numberMapping.get(b)));\n\n        for (int i = 0; i < nums.length; i++) {\n            nums[i] = numsBoxed[i];\n        }\n\n        return nums;\n    }\n\n    private int translateInteger(int num, int[] mapping) {\n        char[] digits = Integer.toString(num).toCharArray();\n        for (int i = 0; i < digits.length; i++) {\n            digits[i] = (char) (\'0\' + mapping[digits[i] - \'0\']);\n        }\n        return Integer.parseInt(new String(digits));\n    }\n}\n```\n\n\n# 2\uFE0F\u20E3\uD83D\uDE0E Approach 2: Directly Translate Integer\n\n# \uD83E\uDD14 Intuition\n- As I said in previous approach we can avoid converting integer to any other data type and convert integer directly, though other logic will be the same and this is only a matter of one function implementation.\n- We will translate integer digit-by-digit starting from the last one.\n\n# \uD83D\uDC69\uD83C\uDFFB\u200D\uD83D\uDCBB Coding \n- Inside `sortJumbled`, define a helper function `translate_integer`:\n  - Handle the edge case where `num` is 0 by directly returning the mapped value of 0.\n  - Initialize `res` to 0 for storing the translated number and `cur_mult` to 1 for constructing the number.\n  - Use a while loop to process each digit of `num`:\n    - Extract the last digit of `num` using modulo 10.\n    - Update `num` by removing the last digit using integer division by 10.\n    - Add the mapped value of the digit to `res`, scaled by `cur_mult`.\n    - Multiply `cur_mult` by 10 to move to the next digit place.\n  - Return the constructed translated integer `res`.\n- Create an empty hashmap `number_mapping`.\n- Iterate over each number in `nums`:\n  - Use the `translate_integer` function to convert the number.\n  - Store the result in `number_mapping` with the original number as the key.\n- Sort the `nums` array using a lambda function that sorts based on the translated values from `number_mapping`.\n- Return the sorted `nums` array.\n\n# \uD83D\uDCD2 Complexity Analysis\n- \u23F0 Time complexity: O(n * log n), since we still use built-in sort functions and change every number digit by digit (though I believe that due to the inner implementation (integers are something computer understands in bits) work with integers are way faster)\n- \uD83E\uDDFA Space complexity: O(n), since we still use `number_mapping` hashmap\n\n# \uD83D\uDCBB Code\n``` python []\nclass Solution:\n    def sortJumbled(self, mapping: list[int], nums: list[int]) -> list[int]:\n        def translate_integer(num: int) -> int:\n            if num == 0:  # edge case, we don\'t want return 0 but mapping value\n                return mapping[0]\n            res: int = 0\n            cur_mult: int = 1  # to construct number we need every time multiple new digit\n            while num > 0:\n                digit = num % 10\n                num //= 10\n                res = mapping[digit] * cur_mult + res\n                cur_mult *= 10\n\n            return res\n\n        number_mapping: dict[int, int] = {}\n        for num in nums:\n            number_mapping[num] = translate_integer(num)\n        nums.sort(key=lambda val: number_mapping[val])\n\n        return nums\n```\n``` C++ []\nclass Solution {\npublic:\n    vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {\n        auto translate_integer = [&](int num) -> int {\n            if (num == 0) {\n                return mapping[0];\n            }\n            int res = 0;\n            int cur_mult = 1;\n            while (num > 0) {\n                int digit = num % 10;\n                num /= 10;\n                res += mapping[digit] * cur_mult;\n                cur_mult *= 10;\n            }\n            return res;\n        };\n\n        unordered_map<int, int> number_mapping;\n        for (int num : nums) {\n            if (number_mapping.find(num) == number_mapping.end()) {\n                number_mapping[num] = translate_integer(num);\n            }\n        }\n\n        sort(nums.begin(), nums.end(), [&](int a, int b) {\n            return number_mapping[a] < number_mapping[b];\n        });\n\n        return nums;\n    }\n};\n```\n``` JavaScript []\nvar sortJumbled = function(mapping, nums) {\n    const translateInteger = (num) => {\n        if (num === 0) {\n            return mapping[0];\n        }\n        let res = 0;\n        let curMult = 1;\n        while (num > 0) {\n            const digit = num % 10;\n            num = Math.floor(num / 10);\n            res += mapping[digit] * curMult;\n            curMult *= 10;\n        }\n        return res;\n    };\n\n    const numberMapping = {};\n    for (let num of nums) {\n        if (!(num in numberMapping)) {\n            numberMapping[num] = translateInteger(num);\n        }\n    }\n\n    nums.sort((a, b) => numberMapping[a] - numberMapping[b]);\n\n    return nums;\n};\n```\n``` Java []\nclass Solution {\n    public int[] sortJumbled(int[] mapping, int[] nums) {\n        Map<Integer, Integer> numberMapping = new HashMap<>();\n\n        for (int num : nums) {\n            if (!numberMapping.containsKey(num)) {\n                numberMapping.put(num, translateInteger(num, mapping));\n            }\n        }\n\n        Integer[] numsBoxed = Arrays.stream(nums).boxed().toArray(Integer[]::new);\n        Arrays.sort(numsBoxed, (a, b) -> Integer.compare(numberMapping.get(a), numberMapping.get(b)));\n\n        for (int i = 0; i < nums.length; i++) {\n            nums[i] = numsBoxed[i];\n        }\n\n        return nums;\n    }\n\n    private int translateInteger(int num, int[] mapping) {\n        if (num == 0) {\n            return mapping[0];\n        }\n        int res = 0;\n        int curMult = 1;\n        while (num > 0) {\n            int digit = num % 10;\n            num /= 10;\n            res += mapping[digit] * curMult;\n            curMult *= 10;\n        }\n        return res;\n    }\n}\n```\n\n## \uD83D\uDCA1\uD83D\uDCA1\uD83D\uDCA1I encourage you to check out [my profile](https://leetcode.com/heir-of-god/) and [Project-S](https://github.com/Heir-of-God/Project-S) project for detailed explanations and code for different problems (not only Leetcode). Happy coding and learning!\uD83D\uDCDA\n\n### Please consider *upvote*\u2B06\uFE0F\u2B06\uFE0F\u2B06\uFE0F because I try really hard not just to put here my code and rewrite testcase to show that it works but explain you WHY it works and HOW. Thank you\u2764\uFE0F\n\n## If you have any doubts or questions feel free to ask them in comments. I will be glad to help you with understanding\u2764\uFE0F\u2764\uFE0F\u2764\uFE0F\n\n![There is image for upvote](https://assets.leetcode.com/users/images/fde74702-a4f6-4b9f-95f2-65fa9aa79c48_1716995431.3239644.png)\n
['Array', 'Hash Table', 'Math', 'String', 'Sorting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']