title: Finding the lexicographically smallest palindrome
body: <p>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).</p>
  
  <p>The task is to convert a given string into a palindrome(IF POSSIBLE). The input format is</p>
  
  <pre><code>a.bc    
  .aacc   
  </code></pre>
  
  <p>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 <code>.</code></p>
  
  <p>Here is my code:</p>
  
  <pre><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 &lt; n ; i++)
  {
      p=0;
      for(j = 0 ; j &lt; 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 &lt; n ; i++)
   {
    for(j = 0 ; j &lt; 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;
          }
        }
      }             
      }
  </code></pre>
  
  <p>And the updated one:</p>
  
  <pre><code> 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 &lt; n ; i++)
      {
          a[i] = r.next(); 
          if(a[i] == " ")
              a[i] = null;
      }       
  
      //FOR INITIALISING THE ARRAY
      for(i = 0 ; i &lt; n ; i++)
      {
          for( j = 0 ; j &lt; a[i].length() ; j++)
          {
              b[i][j] = a[i].charAt(j);
          }
      }
  
    int ll;
    int pp = 0;
    for(i=0;i&lt;n;i++)
    {
        int lena = a[i].length();
        pp = lena;
        ll = 0;
        for(j=0;j&lt;(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 &lt; n ; i++)
    {
        for(j= 0 ; b[i][j]!='\0' ; j++)
        {
            System.out.println(b[i][j]);
        }
    }*/
  
    for(i = 0 ; i &lt; n ; i++)
      {
      int len = a[i].length();
      for( j = 0 ; j &lt; len/2 ; j++)
          {
              if(len%2 == 0)
              {
                  if(b[i][j] == '.' &amp;&amp; b[i][len-1-j] == '.')
                      b[i][j] = b[i][len-1-j] = 'a';
  
                  else if(b[i][j] == '.' &amp;&amp;  b[i][len-1-j]!='.' ) 
                      b[i][j] = b[i][len-1-j];
  
                  else if (b[i][j]!= '.' &amp;&amp;  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] == '.' &amp;&amp; b[i][len-1-j] == '.')
                  {
                      b[i][j] = b[i][len-1-j] = 'a';
                      //System.out.println("ITS WORKING");
                  }
  
                  else if(b[i][j] == '.' &amp;&amp;  b[i][len-1-j]!='.' )
                      b[i][j] = b[i][len-1-j];
  
                  else if (b[i][j]!= '.' &amp;&amp;  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 &lt; n ; i++)
    {
        for(j= 0 ; b[i][j]!='\0' ; j++)
        {
            System.out.print(b[i][j]);
        }
        System.out.println();
    }
  
  }
  }
  </code></pre>