forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
35 lines (33 loc) · 1.03 KB
/
Solution.java
File metadata and controls
35 lines (33 loc) · 1.03 KB
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
package g2301_2400.s2384_largest_palindromic_number;
// #Medium #String #Hash_Table #Greedy #2022_08_25_Time_26_ms_(100.00%)_Space_54.8_MB_(100.00%)
public class Solution {
public String largestPalindromic(String num) {
int[] count = new int[10];
int center = -1;
StringBuilder first = new StringBuilder();
StringBuilder second;
for (char c : num.toCharArray()) {
count[c - '0']++;
}
int c;
for (int i = 9; i >= 0; i--) {
c = 0;
if (count[i] % 2 == 1 && center == -1) {
center = i;
}
if (first.isEmpty() && i == 0) {
continue;
}
while (c < count[i] / 2) {
first.append(i);
c++;
}
}
second = new StringBuilder(first.toString());
if (center != -1) {
first.append(center);
}
first.append(second.reverse());
return first.isEmpty() ? "0" : first.toString();
}
}