String Question 3
Question 3. Write a Program to check a string is a valid palindrome or not.
Program-
class Solution { public boolean isPalindrome(String s) { if (s.isEmpty()) { return true; } int left = 0; int right = s.length() - 1; while (left < right) { char leftChar = s.charAt(left); char rightChar = s.charAt(right); if (!Character.isLetterOrDigit(leftChar)) { left++; } else if (!Character.isLetterOrDigit(rightChar)) { right--; } else { if (Character.toLowerCase(leftChar) != Character.toLowerCase(rightChar)) { return false; } left++; right--; } } return true; } } |