InternDoor

A palindrome after one deletion

Mediumstrings · two pointers

Reported in interviews at (where)

A palindrome reads the same from both ends, like "level". Return whether s can be made into a palindrome by deleting at most one of its characters.

A string that is already a palindrome counts: you may delete nothing.

Example 1
Input
s = "abcca"
Output
true

Deleting the "b" leaves "acca".

Example 2
Input
s = "level"
Output
true

It is already a palindrome.

Example 3
Input
s = "abcd"
Output
false

Every single deletion still leaves letters that do not match.

Limits

  • 1 <= s.length <= 100000
  • s holds lowercase English letters

Hints

Hint 1

Compare from both ends inward. While the letters match, nothing needs deleting.

Hint 2

At the first mismatch one of those two letters must be the deleted one. Check whether the rest is a palindrome after skipping the left one, or after skipping the right one. No further deletion is allowed.

Where this was reported

Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.

Run your code to check it on the examples. Submit runs every hidden test.

Ready for the real one?

Engineering internships and entry-level jobs in India, newest first.