Last active
March 24, 2019 16:25
-
-
Save shixiaoyu/2949f452c647e83d63523872b0eab8b1 to your computer and use it in GitHub Desktop.
Valid Palindrome II version 1
This file contains hidden or 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
// 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