BackeasyTwo Pointers Swiggy Zomato

Flexible Palindrome Solution

Problem Statement

A cryptographer checks if a passphrase can become a mirror-phrase (palindrome) by removing at most one character. Return 'true' if possible.

Example 1
Input
racecar
Output
true

Explanation: Step-by-step: 1. Initialize two pointers, one at the start and one at the end of the string. 2. Compare characters at the two pointers. If they are the same, move both pointers towards the center of the string. 3. If the characters are different, check if removing one character would make the string a palindrome. For 'racecar', removing 'c' would make it a palindrome, so return true.

Example 2
Input
abba
Output
false

Explanation: Step-by-step: 1. Initialize two pointers, one at the start and one at the end of the string. 2. Compare characters at the two pointers. If they are the same, move both pointers towards the center of the string. 3. If the characters are different, check if removing one character would make the string a palindrome. For 'abba', removing a character would not make it a palindrome, so return false.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free