Check if a given string can be a palindrome by altering some character without using a library function I was asked in an interview to write code to check if a given string is a palindrome or can be a palindrome by altering some character without using library function.

My logic is to:


Compare 1st char with remaining chars. If it exists, remove both from array using removeElement().
If it does not exist in the char array, then use removeFirstElement() to remove the first element 1 time only.
chackPotentialPalindrom() returns true if a String can be a palindrome, otherwise false.




package package1;

import java.util.Scanner;

public class Palindrom {
    static int  temp=0;
    static char[] cArr;
        static boolean chackPotentialPalindrom(char[] cAr){
            cArr=cAr;
            if(cArr!=null){
                char current=cArr[0];
                for(int i=1;i&lt;cArr.length;i++){
                    if(current==cArr[i]){
                        cArr=removeElement(i);
                        chackPotentialPalindrom(cArr);
                        break;
                    } 
                    }
                if(cAr.length==2){
                if(cAr[0]==cAr[1]){
                    cArr=null;
                }}
                if(temp==0 &amp;&amp; cArr!=null){
                    temp=1;
                    cArr=removeFirstElement(cArr);
                    chackPotentialPalindrom(cArr);
                    }
                }
            if(cArr==null){
                return true;
            }else{
                return false;
            }
        }
        static char[] removeFirstElement(char[] cAr){
            cArr=cAr;
            if(cArr!=null){
            if(cArr.length &gt;1){
            char[] cArrnew=new char[cArr.length-1];
            for(int j=1,k=0;j&lt;cArr.length;j++,k++){
                cArrnew[k]=cArr[j];
            }
            return cArrnew;
            } else {
                return null;
            }
                } else {
                    return null;
                }
        }
        static char[] removeElement(int i){
            if(cArr.length&gt;2){
            char[] cArrnew=new char[cArr.length-2];
            for(int j=1,k=0;j&lt;cArr.length;j++,k++){
                if(i!=j){
                    cArrnew[k]=cArr[j];
                }else{
                    k-=1;
                }
            }
            return cArrnew;}
            else{
                return null;
            }
        }
        public static void main(String[] args) {
            Scanner scn=new Scanner(System.in);
            while(true){
                temp=0;
            String s=scn.next();
            char[] arr=s.toCharArray();
            System.out.println(chackPotentialPalindrom(arr));
            }
        }
    }



Any tips to optimize this code?
I could not write this in an interview as they have given a pen and paper to code.
It took 3 hrs for me to write this. Can I be a developer?
The above looks to be more work than is necessary. Assuming that we are working with all lower case chars, the following should work.

We match the chars at each end, moving inwards on each cycle. Normally, if any do not match, we consider it a fail and break out. If we are allowed a one pair mismatch (i.e. change one character), then we need to keep track of the count of mismatches and if we have 0 or 1 (less than 2) we consider it to be a palindrome.

private static boolean isAlmostPalindrome(String str){
    int diffCount = 0;
    int left = 0;
    int right = str.length() -1;

    while(right&gt;left){
        if (str.charAt(right--)!= str.charAt(left++)){
            diffCount++;
        }
    }
    return diffCount &lt;2;
}