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

Binary Number with Alternating Bits LeetCode Solution - The Coding Shala

Java Program to Reverse a String using Stack - The Coding Shala

Time Complexity, Space Complexity, Asymptotic Notations - The Coding Shala

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

Sort an Array According to Count of 1(set) bits - The Coding Shala