-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathLargestNumberAfterDigitSwapsByParity.java
49 lines (41 loc) · 1.66 KB
/
LargestNumberAfterDigitSwapsByParity.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity
// T: O(log(n) log(log(n)))
// S: O(log(n))
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class LargestNumberAfterDigitSwapsByParity {
public int largestInteger(int num) {
final String number = num + "";
final List<Integer> evenDigits = getEvenDigits(number);
final List<Integer> oddDigits = getOddDigits(number);
evenDigits.sort(Comparator.reverseOrder());
oddDigits.sort(Comparator.reverseOrder());
final StringBuilder result = new StringBuilder();
for (int i = 0, evenIndex = 0, oddIndex = 0; i < number.length() ; i++) {
if (isOddDigit(number, i)) {
result.append(oddDigits.get(oddIndex++));
} else {
result.append(evenDigits.get(evenIndex++));
}
}
return Integer.parseInt(result.toString());
}
private boolean isOddDigit(String number, int index) {
return (number.charAt(index) - '0') % 2 == 1;
}
private List<Integer> getEvenDigits(String string) {
final List<Integer> result = new ArrayList<>();
for (int index = 0 ; index < string.length() ; index++) {
if (!isOddDigit(string, index)) result.add(string.charAt(index) - '0');
}
return result;
}
private List<Integer> getOddDigits(String string) {
final List<Integer> result = new ArrayList<>();
for (int index = 0 ; index < string.length() ; index++) {
if (isOddDigit(string, index)) result.add(string.charAt(index) - '0');
}
return result;
}
}