Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a given String is palindrome or not without using any library without loop

Tags:

java

algorithm

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 a library function. Here is my Approach

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<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 && 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 >1){
            char[] cArrnew=new char[cArr.length-1];
            for(int j=1,k=0;j<cArr.length;j++,k++){
                cArrnew[k]=cArr[j];
            }
            return cArrnew;
            } else {
                return null;
            }
                } else {
                    return null;
                }
        }
        static char[] removeElement(int i){
            if(cArr.length>2){
            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?

like image 777
Christian De Lorence Avatar asked Jan 27 '23 10:01

Christian De Lorence


2 Answers

Title says "without loop" but you need to check all symbol pairs, so using recursion, as you have tried, looks reasonable. But you don't check and use results of recursive calls.

Pseudocode might look like (note we don't need to change source data or extract substring):

Edit to provide possibility to alter one char

boolean checkPotentialPalindrom(char[] cAr, start, end, altcnt){
       if (end <= start)
             return true

       if (cAr[start] != cAr[end])
            altcnt = altcnt + 1

       if (altcnt > 1) 
             return false

       return checkPotentialPalindrom(cAr, start + 1, end - 1, altcnt)
 }

and make the first call with arguments 0, len(cAr-1), 0

like image 99
MBo Avatar answered Jan 30 '23 01:01

MBo


Answering to your first question..You have to use recursion to solve this problem. Here is my approach.

public boolean isPalindrom(char[] str, int start, int end) {
    if (end <= start)
        return true;
    if (str[start] != str[end] || arraySize(str) <= 1)
        return false;
    return isPalindrom(str, start + 1, end - 1);
}

public int arraySize(char[] str) {
    int count = 0;
    for (char i : str) {
        count++;
    }
    return count;
}

You have tried to implement this algorithm using loops and you can simplify it like this

public boolean isPalindroms(char[] str) {
    int diffCount = 0;
    int left = 0;
    int right = arraySize(str) - 1;

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

public int arraySize(char[] str) {
    int count = 0;
    for (char i : str) {
        count++;
    }
    return count;
}

The answer for the second question that you have ask is definitely you can be a good developer. Computer programming is a Craft. Not Some Kind of Rocket Science. You have to master it by crafting it.

like image 38
Aravinda Meewalaarachchi Avatar answered Jan 30 '23 00:01

Aravinda Meewalaarachchi