Created
July 22, 2019 14:10
-
-
Save temirnurdin/c5c27457826181bc05fc4958c16f5d09 to your computer and use it in GitHub Desktop.
LeetCode Valid Palindrome Solution
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 boolean isPalindrome(String s) { | |
int i = 0; | |
int j = s.length()-1; | |
boolean result = true; | |
if (s.length() == 0 || s.length() == 1) | |
return true; | |
while(i != j-1) { | |
char tempA = s.charAt(i); | |
char tempB = s.charAt(j); | |
if (isAlfaNumeric(tempA)) { | |
if (isAlfaNumeric(tempB)) { | |
if (toLowerCase(tempA) != toLowerCase(tempB)) { | |
result = false; | |
break; | |
} else { | |
j--; | |
i++; | |
} | |
} else { | |
j--; | |
} | |
} else { | |
i++; | |
} | |
} | |
return result; | |
} | |
private static boolean isAlfaNumeric(char c) { | |
return (c >= 'a' && c <= 'z') || | |
(c >= 'A' && c <= 'Z') || | |
(c >= '0' && c <= '9'); | |
} | |
private static char toLowerCase(char c) { | |
return (c >= 'a' && c <= 'z') ? c : (char)(c - 'A' + 'a'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment