-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHighestValuePalindrome.java
57 lines (49 loc) · 2.09 KB
/
HighestValuePalindrome.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
50
51
52
53
54
55
56
57
// https://www.hackerrank.com/challenges/richie-rich/problem
package strings;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class HighestValuePalindrome {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int operations = scanner.nextInt();
String string = scanner.next();
System.out.println(highestPalindromicNumber(string, operations));
}
private static String highestPalindromicNumber(String string, int operations) {
char[] array = string.toCharArray();
Set<Integer> malformed = malformed(array);
if (operations < malformed.size()) {
return "-1";
}
for (int index = 0 ; index < array.length / 2 && operations > 0 ; index++) {
if (!malformed.contains(index) && array[index] != '9' && operations - 2 >= malformed.size()) {
array[index] = array[array.length - 1 - index] = '9';
operations -= 2;
} else if (malformed.contains(index) && array[index] != '9' && array[array.length - 1 - index] != '9' && operations - 2 >= malformed.size() - 1) {
array[index] = array[array.length - 1 - index] = '9';
malformed.remove(index);
operations -= 2;
} else if (malformed.contains(index)) {
char maxElement = (char) Math.max(array[index], array[array.length - 1 - index]);
array[index] = array[array.length - 1 - index] = maxElement;
malformed.remove(index);
operations--;
}
}
if (operations > 0 && array.length % 2 == 1) {
array[array.length / 2] = '9';
}
return new String(array);
}
private static Set<Integer> malformed(char[] array) {
Set<Integer> set = new HashSet<>();
for (int index = 0 ; index < array.length / 2 ; index++) {
if (array[index] != array[array.length - 1 - index]) {
set.add(index);
}
}
return set;
}
}