I wrote the following code and outputs also come as required. However when I submit my code in a website, it always ends up showing that the answer is wrong. Can anyone find any possible mistakes which are responsible for the errors(which I can't see any).
The task is to convert a given string into a palindrome(IF POSSIBLE). The input format is
a.bc
.aacc
The outputs for the above are -1, two times as the strings can't be converted to palindrome, no matter what you keep in the place of .
Here is my code:
import java.util.*;
class m
{
static int p,o,k,m;
public static void main(String args[])
{
String a[] = new String[500];
int e[] = new int[500];
int i,j;
m o = new m();
Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(i=0;i!=n;i++)
{
a[i] = s.next();
}
//FOR CHECKING NUMBER OF PAIRS
for(i = 0 ; i < n ; i++)
{
p=0;
for(j = 0 ; j < a[i].length() ; j++)
{
if(a[i].charAt(j) == a[i].charAt(a[i].length() - 1 - j))
{
++p;
}
}
e[i] = p;
}
//IF STRING LENGTH IS EVEN
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < a[i].length() ; j++)
{
if(a[i].length()%2 == 0)
{
if(e[i] == (a[i].length() - 2))
{
String h = a[i].replace('.',a[i].charAt(a[i].length() - 1 - a[i].indexOf('.')));
System.out.println(h);
break;
}
else
{
System.out.println("-1");
break;
}
}
//IF STRING LENGTH IS ODD
else
{
if(a[i].indexOf('.') == (((a[i].length() + 1 ) / 2 ) - 1))
{
if(e[i] == (a[i].length() ))
{
String g = a[i].replace('.','a');
System.out.println(g);
break;
}
else
{
System.out.println("-1");
break;
}
}
else if(e[i] == (a[i].length() - 2))
{
String q = a[i].replace('.',a[i].charAt(a[i].length() - a[i].indexOf('.') - 1));
System.out.println(q);
break;
}
else
{
System.out.println("-1");
break;
}
}
}
}
And the updated one:
import java.util.*;
import java.lang.*;
class m
{
public static void main(String args[])
{
int i,j;
Scanner r = new Scanner(System.in);
char b[][] = new char [50][50];
String a[] = new String [12345];
int n = r.nextInt();
for( i = 0 ; i < n ; i++)
{
a[i] = r.next();
if(a[i] == " ")
a[i] = null;
}
//FOR INITIALISING THE ARRAY
for(i = 0 ; i < n ; i++)
{
for( j = 0 ; j < a[i].length() ; j++)
{
b[i][j] = a[i].charAt(j);
}
}
int ll;
int pp = 0;
for(i=0;i<n;i++)
{
int lena = a[i].length();
pp = lena;
ll = 0;
for(j=0;j<(lena/2);j++)
{
if(b[i][j] == b[i][lena-1-j])
++ll;
//System.out.println("ITS WORKING" +pp);
}
if(ll == pp/2)
{
System.out.println("-1");
// System.exit(0);
break;
}
}
// System.out.println("ITS WORKING" +count);
//FOR PRINTING THE array
/* for(i = 0 ; i < n ; i++)
{
for(j= 0 ; b[i][j]!='\0' ; j++)
{
System.out.println(b[i][j]);
}
}*/
for(i = 0 ; i < n ; i++)
{
int len = a[i].length();
for( j = 0 ; j < len/2 ; j++)
{
if(len%2 == 0)
{
if(b[i][j] == '.' && b[i][len-1-j] == '.')
b[i][j] = b[i][len-1-j] = 'a';
else if(b[i][j] == '.' && b[i][len-1-j]!='.' )
b[i][j] = b[i][len-1-j];
else if (b[i][j]!= '.' && b[i][len-1-j] == '.')
b[i][len-1-j] = b[i][j];
else{
if(b[i][j] == b[i][len-1-j])
continue;
else
{
System.out.println("-1");
//System.exit(0);
// hh = false;
break;
}
}
}
else if(len%2!=0)
{
if(b[i][(len-1)/2] == '.')
b[i][(len-1)/2] = 'a';
else
{
if(b[i][j] == '.' && b[i][len-1-j] == '.')
{
b[i][j] = b[i][len-1-j] = 'a';
//System.out.println("ITS WORKING");
}
else if(b[i][j] == '.' && b[i][len-1-j]!='.' )
b[i][j] = b[i][len-1-j];
else if (b[i][j]!= '.' && b[i][len-1-j] == '.')
b[i][len-1-j] = b[i][j];
else{
if(b[i][j] == b[i][len-1-j])
continue;
else
{
System.out.println("-1");
//System.exit(0);
// hh = false;
break;
}
}
}
}
}
}
for(i = 0 ; i < n ; i++)
{
for(j= 0 ; b[i][j]!='\0' ; j++)
{
System.out.print(b[i][j]);
}
System.out.println();
}
}
}
When I run your code, it seems to work as expected on input containing exactly 1 dot. With no dots (e.g., abba
) or more than one dot (ab..
) it seems to print -1 always. I cannot tell whether this is as expected (as your web site expects it!). It’s my best guess at what could be wrong.
To handle more than one dot, if it were me I would:
a[i].length() - 1 - j
). If both are dots, fill in 'a' in both places; if one is a dot, copy the letter from the opposite position; if none is a dot, they have to be the same, or you can print -1 and break out of the loop (or maybe better, set a boolean variable to indicate failure to construct a palindrome).If you require further help. I believe we need a more precise problem statement, more precise requirements for your program.