Back to Palindrome
Course Practice
Medium

Valid Palindrome IV

LAB

Return true if changing at most two characters can make s a palindrome.

EXAMPLES

Example 1
Input
{
  "s": "abcdba"
}

Output
true

FUNCTION SHAPE

s: stringbool
SOLUTION NOTE

Note that this simple 2 pointers works because this is 'changes', and not removals. Removals will require DP because we have 2 options to remove, i or j. In this case of changes, we can choose either one to become the other, it doesn't matter which one we choose and it doesn't change our answer as each pair of indices is independent after matching.

Open on LeetCode
00:00
2 local tests readyRun with ⌘/Ctrl + Enter. Your code stays in this browser.

Runs solve(...) locally in a browser worker. SWE Playbook does not submit your code. Only run code you trust; Python code may access the network.