LeetCode 2191: Sort the Jumbled Numbers Solution

Master LeetCode problem 2191 (Sort the Jumbled Numbers), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2191. Sort the Jumbled Numbers

Problem Explanation

Explanation:

To solve this problem, we first need to create a mapping between the original digits and the mapped digits according to the given mapping array. Then, we sort the nums array based on their mapped values.

  1. Create a mapping between original digits and mapped digits using the mapping array.
  2. Sort the nums array based on their mapped values.
  3. Return the sorted nums array.

Time Complexity:

  • Creating the mapping: O(1) since the mapping array always has a fixed size.
  • Sorting the nums array: O(NlogN) where N is the number of elements in the nums array.

Space Complexity:

  • O(1) for the mapping array as its size is fixed.
  • O(N) for the sorted nums array.

:

Solution Code

class Solution {
    public int[] sortArray(int[] mapping, int[] nums) {
        int[] map = new int[10];
        for (int i = 0; i < mapping.length; i++) {
            map[i] = mapping[i];
        }
        
        Arrays.sort(nums, (a, b) -> {
            String sa = mapNumber(a, map);
            String sb = mapNumber(b, map);
            return sa.compareTo(sb);
        });
        
        return nums;
    }
    
    public String mapNumber(int num, int[] map) {
        String str = String.valueOf(num);
        StringBuilder sb = new StringBuilder();
        
        for (char c : str.toCharArray()) {
            sb.append(map[c - '0']);
        }
        
        return sb.toString();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2191 (Sort the Jumbled Numbers)?

This page provides optimized solutions for LeetCode problem 2191 (Sort the Jumbled Numbers) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2191 (Sort the Jumbled Numbers)?

The time complexity for LeetCode 2191 (Sort the Jumbled Numbers) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2191 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2191 in Java, C++, or Python.

Back to LeetCode Solutions