I am getting correct answers for all the test cases mentioned in the comments as well as for the cases mentioned the earlier posts related to this problem but I am still getting WA Can someone plz help…
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int length;
char N[];
try {
BufferedReader sc =new BufferedReader(new InputStreamReader(System.in));
String ip;
try {
ip = sc.readLine();
} catch (Exception e) {
// TODO Auto-generated catch block
ip="";
} //Taking input as an integer
char A[]= ip.toCharArray();
length = A.length;
N = new char[length];
HashMap<Character, Integer> abcd = new HashMap<>();
abcd.put('A', 0);
abcd.put('B', 0);
abcd.put('C', 0);
abcd.put('D', 0);
char prev = ' ';
for(int i = 0; i<length; i++) {
char topInt = A[i];
//check for A-
if(abcd.get('A')!=((double)length)/4 && topInt!='A' && prev!='A') {
N[i]='A';
prev='A';}
else if(abcd.get('B')!=((double)length)/4 && topInt!='B' && prev!='B') {
N[i]='B';
prev='B';}
else if(abcd.get('C')!=((double)length)/4 && topInt!='C' && prev!='C') {
N[i]='C';
prev='C';}
else if(abcd.get('D')!=((double)length)/4 && topInt!='D' && prev!='D') {
N[i]='D';
prev='D';}
char preInt = prev;
if (preInt != ' ')
abcd.put(preInt,abcd.get(preInt)+1);
}
String s ="";
for(int j=0; j<length;j++) {
s=s+N[j];
}
System.out.println(s);} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}