Search code examples
javastringpalindromenumberformatexception

Reduce the value of a letter, e.g. can change 'd' to 'c', but cannot change 'c' to 'd'. In order to form a palindrome


public class Solution {

public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int tc = Integer.parseInt(br.readLine());//I get Numberformat Exception here
    for(int i=0;i<tc;i++)                    // Even if my inputs are on separate lines
        {
    String original = br.readLine();
    palindrome(original);
        }
       }

public static void palindrome(String original)
{
     String reverse="";
    int length = original.length();
        for ( int i = length - 1 ; i >= 0 ; i-- )
        reverse = reverse + original.charAt(i);
    if (original.equals(reverse))
    {
     System.out.println(0);
    }
    else
     {
          char[] org = original.toCharArray();
          int len = org.length;
          int mid = len / 2;

          if(len % 2 == 0)
          {
            char[] front = new char[mid];
            char[] back = new char[mid];
            for(int i=0;i<mid;i++)
            {
                front[i] = org[i];
            }
            int j=0;
            for(int i=len-1;i>=mid;i--)
            {
                back[j] = org[i];
                j++;
                while(j > mid)
                {
                    break;
                }
            }
            change(front,back,mid);
          }
          else
          {
            char[] front = new char[mid];
            char[] back = new char[mid];
            for(int i=0;i<mid;i++)
            {
                front[i] = org[i];
            }
            int j=0;
            for(int i=len-1;i>mid;i--)
            {
                back[j] = org[i];
                j++;
                while(j > mid)
                {
                    break;
                }
            }
            change(front,back,mid);
          }
     }
}
public static void change(char[] front,char[] back,int len)
   {
       int count =0;
       for(int i =0;i<len;i++)
       {
           if(front[i] != back[i] )
            {
                count += (back[i] - front[i]);
            }
        }
     System.out.println(count)
   }
  }
  1. What i try to do here is get an input from the number of test cases say 3 in my first line followed by the test-cases themselves.
  2. sample input :

3

abc abcba abcd

  1. Now it has to check if the string is a palindrome if its so it ll print 0
  2. else it breaks the string into two halves front and back and finds the minimum number of changes to make it a palidrome.
  3. here i have also checked if its a odd or even length string if odd i have omitted the middle char.
  4. By changes we can only change 'd' to 'b' not 'b' to 'd'
  5. Once a letter has been changed to 'a', it can no longer be changed.

My code works fine for the above input but it doesnt for some other inputs i dont quiet understand why..

for instance if i give a custom test case as

5
assfsdgrgregedhthtjh
efasfhnethiaoesdfgv
ehadfghsdfhmkfpg
wsertete
agdsjgtukgtulhgfd

I get a Number Format Exception.


Solution

  • Guys thank you for all your suggestion i just found out why my other test cases weren't working

    public static void change(char[] front,char[] back,int len)
    {
       int count =0;
       for(int i =0;i<len;i++)
       {
           if(front[i] != back[i] )
            {
                count += (back[i] - front[i]);
            }
        }
     System.out.println(count)
    }
    

    This part of my code has to be changed to

    public static void change(char[] front,char[] back,int len)
       {
           int count =0;
           for(int i =0;i<len;i++)
           {
               if(front[i] != back[i] )
                {
                    char great = findGreatest(front[i],back[i]);
                    if(great == back[i])
                        {
                    count += (back[i] - front[i]);
                    }
                   else 
                       {
                       count += (front[i] - back[i]);
                   }
                }
            }
            System.out.println(count);
       }
       public static char findGreatest(char first,char second)
        {
          int great = first;
          if(first < second)
              {
              great = second;
          }
        return (char)great;
        }
    
    1. Because i get negative values coz of subtracting ascii's which are greater than them and as i have already mentioned i can only do 'd' to 'a' not the other way round.

    Thank you for your time guys!