Created
December 3, 2023 05:32
-
-
Save SumitJainUTD/dbf2ae3610aa6f1971ffd60fcda9b7e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Palindrome2 { | |
public static boolean isPalindrome(String input){ | |
if (input==null) | |
return false; | |
if(input.trim()==""){ | |
return true; | |
} | |
int i = 0; | |
int j = input.length()-1; | |
// "Race car!" | |
while(i<j){ | |
char a = input.charAt(i); | |
char b = input.charAt(j); | |
if (!Character.isLetter(a)){ | |
i++; | |
} | |
if (!Character.isLetter(b)){ | |
j--; | |
} | |
if(Character.isLetter(a) && Character.isLetter(b)){ | |
if(!String.valueOf(a).toLowerCase().equals(String.valueOf(b).toLowerCase())){ | |
return false; | |
}else{ | |
i++; | |
j--; | |
} | |
} | |
} | |
return true; | |
} | |
public static void main(String[] args) { | |
System.out.println(isPalindrome("^&A B!C!!!ba")); | |
System.out.println(isPalindrome("*&^Ddd*&^*&")); | |
System.out.println(isPalindrome("^&A d!C!!!ba")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment