Given a string s
, return true
if the s
can be palindrome after deleting at most one character from it.
Example 1:
Input: s = "aba" Output: true
Example 2:
Input: s = "abca" Output: true Explanation: You could delete the character 'c'.
Example 3:
Input: s = "abc" Output: false
Constraints:
1 <= s.length <= 105
s
consists of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for this problem is to consider every possible string that could be formed by removing one character. We simply check each of these strings to see if they are palindromes. If we find a palindrome, we have our answer.
Here's how the algorithm would work step-by-step:
def valid_palindrome_two(input_string):
string_length = len(input_string)
def is_palindrome(substring):
left_pointer = 0
right_pointer = len(substring) - 1
while left_pointer < right_pointer:
if substring[left_pointer] != substring[right_pointer]:
return False
left_pointer += 1
right_pointer -= 1
return True
# First, check if the original string is a palindrome
if is_palindrome(input_string):
return True
for index_to_remove in range(string_length):
# Create a new string with one character removed
modified_string = input_string[:index_to_remove] + input_string[index_to_remove+1:]
# Check if the modified string is a palindrome
# If it is, we've found a valid palindrome with one removal
if is_palindrome(modified_string):
return True
# If none of the removals resulted in a palindrome,
# then the original string cannot be made a palindrome
return False
The fast way to check if a string can become a palindrome by removing at most one character is to compare the string from both ends. If we find a mismatch, we'll check if the string is a palindrome by skipping one character from the left or from the right.
Here's how the algorithm would work step-by-step:
def validPalindrome(input_string: str) -> bool:
def isPalindrome(input_string: str, start_index: int, end_index: int) -> bool:
while start_index < end_index:
if input_string[start_index] != input_string[end_index]:
return False
start_index += 1
end_index -= 1
return True
start_index = 0
end_index = len(input_string) - 1
while start_index < end_index:
if input_string[start_index] != input_string[end_index]:
# Mismatch found; check both removal options.
#Check by removing the character at start_index.
is_palindrome_skipping_start = isPalindrome(input_string, start_index + 1, end_index)
#Check by removing the character at end_index.
is_palindrome_skipping_end = isPalindrome(input_string, start_index, end_index - 1)
#If either is a palindrome, return True.
return is_palindrome_skipping_start or is_palindrome_skipping_end
start_index += 1
end_index -= 1
#If no mismatches, it's already a palindrome.
return True
Case | How to Handle |
---|---|
Null or empty string | Return true; an empty string is considered a palindrome. |
String of length 1 | Return true; a single-character string is a palindrome. |
String of length 2, palindrome | Return true; it's already a palindrome without deletion. |
String of length 2, not a palindrome | Return true; removing either character makes it a palindrome. |
Very long string | Ensure the solution has O(n) time complexity (e.g., two-pointer approach) to avoid timeouts. |
String that is already a palindrome | Return true; no deletion is needed. |
String with mixed case letters (e.g., 'Racecar') | Convert the string to lowercase or uppercase before checking for palindrome properties. |
String with non-alphanumeric characters (e.g., 'A man, a plan, a canal: Panama') | Filter out non-alphanumeric characters before checking for palindrome properties; alternatively, adjust the palindrome check to only consider alphanumeric characters. |