Check if given String is Palindrome or not - The Coding Shala

Home >> Programming Questions >> Check if String is Palindrome or not

Check if given String is Palindrome or not

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:
Input: "race a car"
Output: false

Palindrome String Java Program

Method 1:

We will check char if they are alphanumeric then will compare them.

Java Code:

class Solution {
    public boolean isPalindrome(String s) {
        int i=0;
        int len = s.length()-1;
        s = s.toUpperCase();
        System.out.println(s);
        while(i<=len){
            if(!Character.isLetterOrDigit(s.charAt(i))) i++;
            else if(!Character.isLetterOrDigit(s.charAt(len))) len--;
            else{
                if(s.charAt(i) != s.charAt(len)) return false;
                i++;
                len--;
            }
        }
        return true;
    }
}



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Java Program to Convert Binary to Decimal - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala