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();
            if(!Character.isLetterOrDigit(s.charAt(i))) i++;
            else if(!Character.isLetterOrDigit(s.charAt(len))) len--;
                if(s.charAt(i) != s.charAt(len)) return false;
        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.


Popular Posts from this Blog

LeetCode - Crawler Log Folder Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala

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

First Unique Character in a String Java - The Coding Shala

Add two numbers in Scala - The Coding Shala