Ctrl K

Palindrome in One or Two Edits

Medium
You are given a word word of lowercase Latin letters. One edit replaces any single letter of the word with a different letter. Return True if the word can be turned into a palindrome using **exactly one or exactly two** edits, and False otherwise.
Example 1
word='radiar' → True

Only the middle pair differs: "d" and "i". Change "i" to "d" and you get "raddar".

Example 2
word='noon' → True

The word is already a palindrome, but at least one edit is required: change both "o"s to "e" and "neen" is still a palindrome.

Example 3
word='abcxyz' → False

All three mirrored pairs differ and each needs its own change, so two edits are not enough.

Constraints
  • 1 ≤ word.length ≤ 10⁵
  • word consists of lowercase Latin letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Mirror Fragment MediumWords in Reverse Order MediumExactly One Edit Apart MediumWhich Release Is Newer MediumReverse the Word Order in Place Medium
loading Python…
Input
word='radiar'
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 142 tests on the server, hidden ones included.