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

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Add two numbers in Scala - The Coding Shala