-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
114 lines (102 loc) · 3.37 KB
/
Solution.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int numberOfDigits = reader.nextInt();
int maximumDigitsToBeAltered = reader.nextInt();
String inputString = reader.next();
String result = richieRich(inputString, numberOfDigits, maximumDigitsToBeAltered);
System.out.println(result);
}
static String richieRich(String inputString, int numberOfDigits, int maximumDigitsToBeAltered) {
String palindrome = "";
int allDigitsWithoutNine = inputString.replaceAll("9", "").length();
if (allDigitsWithoutNine <= maximumDigitsToBeAltered) {
String regex = "[0-8]";
palindrome = inputString.replaceAll(regex, "9");
return palindrome;
}
StringBuilder strBuilder = new StringBuilder(inputString);
int nonMatches = 0;
for (int i = 0; i < numberOfDigits / 2; i++) {
int mirrorIndex = numberOfDigits - 1 - i;
if (strBuilder.charAt(i) != strBuilder.charAt(mirrorIndex)) {
nonMatches++;
}
}
int indexOfTransition = 0;
if (nonMatches < maximumDigitsToBeAltered) {
for (int i = 0; i < numberOfDigits / 2; i++) {
int mirrorIndex = numberOfDigits - 1 - i;
if (strBuilder.charAt(i) != '9' && strBuilder.charAt(mirrorIndex) != '9') {
if (strBuilder.charAt(i) != strBuilder.charAt(mirrorIndex)) {
nonMatches--;
}
if (nonMatches > maximumDigitsToBeAltered - 2) {
indexOfTransition = i;
break;
}
strBuilder.setCharAt(i, '9');
strBuilder.setCharAt(mirrorIndex, '9');
maximumDigitsToBeAltered -= 2;
if (nonMatches == maximumDigitsToBeAltered) {
indexOfTransition = i + 1;
break;
}
} else if (strBuilder.charAt(i) != strBuilder.charAt(mirrorIndex)) {
if (Character.getNumericValue(strBuilder.charAt(i)) > Character
.getNumericValue(strBuilder.charAt(mirrorIndex))) {
strBuilder.setCharAt(mirrorIndex, strBuilder.charAt(i));
maximumDigitsToBeAltered--;
nonMatches--;
} else {
strBuilder.setCharAt(i, strBuilder.charAt(mirrorIndex));
maximumDigitsToBeAltered--;
nonMatches--;
}
}
}
}
for (int i = indexOfTransition; i < numberOfDigits / 2; i++) {
int mirrorIndex = numberOfDigits - 1 - i;
if (strBuilder.charAt(i) != strBuilder.charAt(mirrorIndex)) {
if (Character.getNumericValue(strBuilder.charAt(i)) > Character
.getNumericValue(strBuilder.charAt(mirrorIndex))) {
strBuilder.setCharAt(mirrorIndex, strBuilder.charAt(i));
maximumDigitsToBeAltered--;
if (maximumDigitsToBeAltered == 0) {
break;
}
} else {
strBuilder.setCharAt(i, strBuilder.charAt(mirrorIndex));
maximumDigitsToBeAltered--;
if (maximumDigitsToBeAltered == 0) {
break;
}
}
}
}
if (!isPalindrome(strBuilder.toString())) {
return "-1";
}
if (strBuilder.toString().length() % 2 != 0
&& maximumDigitsToBeAltered>0) {
int midIndex = strBuilder.toString().length() / 2;
if (strBuilder.charAt(midIndex) != '9') {
strBuilder.setCharAt(midIndex, '9');
}
}
palindrome = strBuilder.toString();
return palindrome;
}
public static boolean isPalindrome(String inputString) {
StringBuilder strBuilder = new StringBuilder(inputString);
if (strBuilder.reverse().toString().equals(inputString)) {
return true;
}
return false;
}
}