Untitled

mail@pastecode.io avatar
unknown
java
22 days ago
1.2 kB
4
Indexable
Never
class HelloWorld {
    public static void main(String[] args) {
        String str = "ABCA";
        System.out.print(checkPalindrome(str));
    }
    
    private static boolean checkPalindrome(String s) {
        // Check if complete string is a palindrome
        if (isPalindrome(s))
            return true;

        // Check by removing each index
        for (int i=0; i< s.length(); i++) {
            String str = stringWithSpecificIndexRemoved(s, i);
            if (isPalindrome(str))
                return true;
        }
        return false;
    }
    
    private static boolean isPalindrome(String s) {
        int start = 0;
        int end = s.length() - 1;
        
        while (start < end) {
            if (s.charAt(start) != s.charAt(end))
                return false;
            else {
                start++;
                end--;
            }
        }
        return true;
    }
    
    private static String stringWithSpecificIndexRemoved(String s, int index) {
        String str = "";
        for (int i=0; i< s.length(); i++) {
            if (i == index)
                str += "";
            else
                str += s.charAt(i);
        }
        return str;
    }
}
Leave a Comment