KatInterview

Valid Palindrome with At Most One Removal

StringsAuto-graded · 23 test cases
EasyAuto-gradedStringsstringstwo-pointers

Last updated

Determine whether a given string can be converted into a palindrome by removing no more than one character.

Given a string s containing lowercase letters, determine if it is possible to transform s into a palindrome by removing at most one character from any position.

A string is a palindrome if it reads identically from left to right and right to left. Removing zero characters is permitted, meaning any string that is already a palindrome satisfies the condition.

Constraints

  • 1 <= len(s) <= 10^5
  • s consists exclusively of lowercase English characters (a-z).

Examples

Example 1

Input: can_be_palindrome_after_one_deletion('abca')

Output: True

Example 2

Input: can_be_palindrome_after_one_deletion('abcbaa')

Output: True

Example 3

Input: can_be_palindrome_after_one_deletion('abc')

Output: False

Hints

Hint 1

Use two pointers starting from opposite ends of the string and move inward as long as the characters match.

Hint 2

When you encounter the first mismatch at indices `left` and `right`, you must remove either `s[left]` or `s[right]` to proceed.

Hint 3

Check if the remaining substring—either `s[left+1 .. right]` or `s[left .. right-1]`—is a strict palindrome without any further removals.