HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Check if a given string can be a palindrome by altering some character without using a library function

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
canwithoutgivencharacterfunctionpalindromesomeusingalteringlibrary

Problem

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;i1){
            char[] cArrnew=new char[cArr.length-1];
            for(int j=1,k=0;j2){
            char[] cArrnew=new char[cArr.length-2];
            for(int j=1,k=0;j<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?

Solution

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>left){
        if (str.charAt(right--)!= str.charAt(left++)){
            diffCount++;
        }
    }
    return diffCount <2;
}

Code Snippets

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

    while(right>left){
        if (str.charAt(right--)!= str.charAt(left++)){
            diffCount++;
        }
    }
    return diffCount <2;
}

Context

StackExchange Code Review Q#88317, answer score: 2

Revisions (0)

No revisions yet.