-
Notifications
You must be signed in to change notification settings - Fork 0
/
JumbledNumber.java
28 lines (23 loc) · 939 Bytes
/
JumbledNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class JumbledNumber {
public int[] sortJumbled(int[] mapping, int[] nums) {
Map<Integer, Integer> numberMapping = new HashMap<>();
for (int num : nums) {
if (!numberMapping.containsKey(num)) {
numberMapping.put(num, translateInteger(num, mapping));
}
}
Integer[] numsBoxed = Arrays.stream(nums).boxed().toArray(Integer[]::new);
Arrays.sort(numsBoxed, (a, b) -> Integer.compare(numberMapping.get(a), numberMapping.get(b)));
for (int i = 0; i < nums.length; i++) {
nums[i] = numsBoxed[i];
}
return nums;
}
private int translateInteger(int num, int[] mapping) {
char[] digits = Integer.toString(num).toCharArray();
for (int i = 0; i < digits.length; i++) {
digits[i] = (char) ('0' + mapping[digits[i] - '0']);
}
return Integer.parseInt(new String(digits));
}
}