Skip to content

Instantly share code, notes, and snippets.

@temirnurdin
Created July 22, 2019 14:10
Show Gist options
  • Save temirnurdin/c5c27457826181bc05fc4958c16f5d09 to your computer and use it in GitHub Desktop.
Save temirnurdin/c5c27457826181bc05fc4958c16f5d09 to your computer and use it in GitHub Desktop.
LeetCode Valid Palindrome Solution
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