680. Valid Palindrome II
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks if a given string s can become a palindrome by deleting at most one character. This means we have two cases to consider:
Solution
Valid Palindrome II in Python
class Solution:
def validPalindrome(self, s: str) -> bool:
# Helper function to check if a substring is a palindrome
def check_palindrome(l, r):
while l < r:
if s[l] != s[r]:
return False
l += 1
r -= 1
return True
left, right = 0, len(s) - 1
while left < right:
# Find the first pair of mismatched characters.
if s[left] != s[right]:
# If a mismatch is found, we have two options:
# 1. Skip the character at the left pointer and check the rest.
# 2. Skip the character at the right pointer and check the rest.
# If either results in a palindrome, the original condition is met.
return check_palindrome(left + 1, right) or check_palindrome(left, right - 1)
left += 1
right -= 1
# If the loop completes, the string is already a palindrome.
return True
Valid Palindrome II in Java
class Solution {
public boolean validPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
// Find the first pair of mismatched characters.
if (s.charAt(left) != s.charAt(right)) {
// If a mismatch is found, we have two options:
// 1. Skip the character at the left pointer.
// 2. Skip the character at the right pointer.
// We check if either of these modified substrings is a palindrome.
return isPalindrome(s, left + 1, right) || isPalindrome(s, left, right - 1);
}
left++;
right--;
}
// If the loop completes without finding a mismatch, the string is already a palindrome.
return true;
}
// Helper method to check if a substring is a palindrome.
private boolean isPalindrome(String s, int left, int right) {
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
Valid Palindrome II in C++
class Solution {
public:
bool validPalindrome(string s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
// Find the first pair of mismatched characters.
if (s[left] != s[right]) {
// After finding a mismatch, we have two possibilities:
// 1. The substring s[left+1...right] is a palindrome.
// 2. The substring s[left...right-1] is a palindrome.
// If either is true, the original string can be made a palindrome by one deletion.
return isPalindrome(s, left + 1, right) || isPalindrome(s, left, right - 1);
}
left++;
right--;
}
// If the loop completes, the string is already a palindrome.
return true;
}
private:
// Helper function to check if a substring is a palindrome.
bool isPalindrome(const string& s, int left, int right) {
while (left < right) {
if (s[left] != s[right]) {
return false;
}
left++;
right--;
}
return true;
}
};
Valid Palindrome II in TypeScript
function validPalindrome(s: string): boolean {
// Helper function to check if a substring defined by a range is a palindrome.
const isPalindromeRange = (left: number, right: number): boolean => {
while (left < right) {
if (s[left] !== s[right]) {
return false;
}
left++;
right--;
}
return true;
};
let left = 0;
let right = s.length - 1;
while (left < right) {
// Find the first pair of mismatched characters.
if (s[left] !== s[right]) {
// Once a mismatch is found, we check two scenarios:
// 1. Removing the character at `left` and checking if the rest is a palindrome.
// 2. Removing the character at `right` and checking if the rest is a palindrome.
// If either is true, the condition is met.
return isPalindromeRange(left + 1, right) || isPalindromeRange(left, right - 1);
}
left++;
right--;
}
// If the loop completes, the string is already a palindrome.
return true;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.