Skip to content

Instantly share code, notes, and snippets.

@shixiaoyu
Last active March 24, 2019 16:25
Show Gist options
  • Save shixiaoyu/2949f452c647e83d63523872b0eab8b1 to your computer and use it in GitHub Desktop.
Save shixiaoyu/2949f452c647e83d63523872b0eab8b1 to your computer and use it in GitHub Desktop.
Valid Palindrome II version 1
// A quick implementation, a bit duplicate on checking on the palindrome part
public boolean validPalindrome(String s) {
// It says s is non-empty, the other is covered by normal case, so don't need this check
if (s == null || s.length() <= 2) {
return true;
}
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (s.charAt(i) == s.charAt(j)) {
i++;
j--;
continue;
}
return isPalindromeHelper(s, i, j - 1) || isPalindromeHelper(s, i + 1, j);
}
return true;
}
public boolean isPalindromeHelper(String s, int start, int end) {
while (start < end) {
if (s.charAt(start) == s.charAt(end)) {
start++;
end--;
continue;
}
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment