-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackerRankRichieRich.java
More file actions
69 lines (59 loc) · 2.12 KB
/
hackerRankRichieRich.java
File metadata and controls
69 lines (59 loc) · 2.12 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
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
package javaBackjoon;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class hackerRankRichieRich {
static String richieRich(String s, int n, int k){
int sLen = n, changeCnt = k;
int diffCnt = 0;
char[] result = new char[sLen];
for(int i = 0, j = sLen - 1; i < (sLen / 2); i++, j--) {
if(s.charAt(i) != s.charAt(j)) diffCnt++;
}
if(diffCnt > changeCnt)
return "-1";
for(int i = 0, j = sLen - 1; i < (sLen / 2); i++, j--) {
if(s.charAt(i) != s.charAt(j)) {
if(diffCnt == changeCnt) {
result[i] = (s.charAt(i) > s.charAt(j) ? s.charAt(i) : s.charAt(j));
result[j] = (s.charAt(i) > s.charAt(j) ? s.charAt(i) : s.charAt(j));
changeCnt--;
}
else {
result[i] = '9';
result[j] = '9';
// if one of char is 9 then just change one thing so changeCnt--
if (s.charAt(i) == '9' || s.charAt(j) == '9')
changeCnt--;
else
changeCnt -= 2;
}
diffCnt--;
}
else if(changeCnt >= 2 && (diffCnt <= changeCnt - 2) && s.charAt(i) != '9') {
result[i] = '9';
result[j] = '9';
changeCnt -= 2;
}
else {
result[i] = s.charAt(i);
result[j] = s.charAt(j);
}
}
if(sLen%2 == 1) {
if(changeCnt > 0) result[sLen/2] = '9';
else result[sLen/2] = s.charAt(sLen/2);
}
return String.valueOf(result);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
String s = in.next();
String result = richieRich(s, n, k);
System.out.println(result);
}
}