Created
April 11, 2017 15:31
-
-
Save zGrav/6bfc2c97688d89a1de17cfda471bf150 to your computer and use it in GitHub Desktop.
palindromesolution
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
class Solution { | |
public static void main(String[] args) { | |
String PString = "0 man, a plan, a canal: Panam0"; | |
String nonPString = "race a car"; | |
System.out.println("is PString a Palindrome? : " + isPalindrome(PString)); | |
System.out.println("is nonPString a Palindrome? : " + isPalindrome(nonPString)); | |
} | |
public static boolean isPalindrome(String str) { | |
int size = str.length(); | |
int left = 0; | |
int right = size - 1; | |
while (right >= left) { | |
if (str.toLowerCase().charAt(right) == str.toLowerCase().charAt(left)) { | |
right--; | |
left++; | |
} else if (!Character.isLetter(str.charAt(right))) { | |
right--; | |
} else if (!Character.isLetter(str.charAt(left))) { | |
left++; | |
} else { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment