Given a non-empty string s
, you may delete at most one character. Judge whether you can make it a palindrome.
Example 1:
Input: "aba" Output: True
Example 2:
Input: "abca" Output: True Explanation: You could delete the character 'c'.
Note:
- The string will only contain lowercase characters a-z.
The maximum length of the string is 50000.
class Solution:
def validPalindrome(self, s: str) -> bool:
def isPalindrome(s):
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return isPalindrome(s[i: j]) or isPalindrome(s[i + 1: j + 1])
i += 1
j -= 1
return True
class Solution {
public boolean validPalindrome(String s) {
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return isPalindrome(s.substring(i, j)) || isPalindrome(s.substring(i + 1, j + 1));
}
}
return true;
}
private boolean isPalindrome(String s) {
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}
function validPalindrome(s: string): boolean {
for (let i: number = 0, j = s.length - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return isPalinddrome(s.slice(i, j)) || isPalinddrome(s.slice(i + 1, j + 1));
}
}
return true;
};
function isPalinddrome(s: string): boolean {
for (let i: number = 0, j = s.length - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}