SUPER EASY 😂 | BEATS 100% 👌| EK DUM LALLU 🎯| LOLLIPOP SOLUTION 🍭|
\n\n\n#### Sorting Array Based on Mapped Values with Stable Sorting in Dart\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nMy fir
![image.png](https://assets.leetcode.com/users/images/1e8cf311-2924-4055-8f8f-6bc40e9859c6_1721834495.44884.png)\n\n\n#### Sorting Array Based on Mapped Values with Stable Sorting in Dart\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy first thought was to transform the given numbers according to the mapping and then sort the numbers based on their transformed values. Since we need to maintain the relative order of elements with the same mapped value, a stable sorting algorithm is essential.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Create a map (`rec`) to store the digit mapping from the given array.\n2. For each number in the `nums` array, compute its mapped value by converting each digit according to the `rec` map.\n3. Store these original numbers along with their corresponding mapped values as pairs in a list.\n4. Sort this list of pairs based on the mapped values. If two mapped values are the same, maintain the relative order from the original list.\n5. Extract the original numbers from the sorted list of pairs to get the final sorted array.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity is \\(O(n \\log n)\\), where \\(n\\) is the number of elements in the `nums` array. This is due to the sorting step, which dominates the overall complexity.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is \\(O(n)\\) because we need additional space to store the mapped values and pairs of original numbers and their mapped values.\n\n# Code\n```dart\nclass Solution {\n  List<int> sortJumbled(List<int> mapping, List<int> nums) {\n    // Create a map for the digit mapping\n    Map<int, int> rec = {};\n    for (int i = 0; i < mapping.length; i++) {\n      rec[i] = mapping[i];\n    }\n\n    // Create a list of pairs (original number, mapped value)\n    List<MapEntry<int, int>> mappedNums = [];\n    for (int nm in nums) {\n      String numStr = nm.toString();\n      String newNumStr = numStr.split("").map((digit) => rec[int.parse(digit)]).join();\n      int newNum = int.parse(newNumStr);\n      mappedNums.add(MapEntry(nm, newNum));\n    }\n\n    // Sort the list of pairs based on the mapped values, maintaining relative order for same values\n    mappedNums.sort((a, b) {\n      if (a.value != b.value) {\n        return a.value.compareTo(b.value);\n      } else {\n        return nums.indexOf(a.key).compareTo(nums.indexOf(b.key));\n      }\n    });\n\n    // Extract the sorted original numbers\n    List<int> sortedNums = mappedNums.map((entry) => entry.key).toList();\n    return sortedNums;\n  }\n}\n\n/*\nExample Walkthrough\nGiven mapping = [8,9,4,0,2,1,3,5,7,6] and nums = [991,338,38]:\n\nMapping:\n\n991 becomes 669.\n338 becomes 007 (or 7 after removing leading zeros).\n38 becomes 07 (or 7 after removing leading zeros).\nCreating Pairs:\n\nPairs: [(991, 669), (338, 7), (38, 7)].\nSorting:\n\nSort primarily by the mapped values.\nIf mapped values are equal (7 for 338 and 38), maintain the original order (338 before 38).\nResult:\n\nThe sorted array is [338, 38, 991].\nThis ensures the elements are sorted based on their mapped values while maintaining the original relative order for elements with the same mapped value.\n*/\n\n```\n\nThis solution ensures that the `nums` array is sorted based on their mapped values while maintaining the original relative order for elements with the same mapped values.\n\n![image.png](https://assets.leetcode.com/users/images/30296168-f8cc-4cbb-b303-a3dc5082e71b_1721839184.8289688.png)\n\n\n
['Dart']