Check if given String is Palindrome or not - The Coding Shala
Home >> Programming Questions >>  Check if String is Palindrome or not
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.
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
 
Comments
Post a Comment