Strings String basics

124. Valid Palindrome II

Easy

You are given a string s of lowercase letters. You may delete at most one character from it. Return true if s can then read the same from left to right as from right to left, and false otherwise.

"At most one" includes deleting nothing, so a string that already reads the same both ways gives true. The character you delete can sit anywhere in s, the first and last positions included.

Example 1

Input: s = "racelcar"

Output: true

Explanation: The first three letters mirror the last three, but the e and the l in the middle differ. Deleting the l leaves "racecar", which reads the same both ways.

Example 2

Input: s = "refer"

Output: true

Explanation: It already reads the same both ways, so nothing needs deleting.

Example 3

Input: s = "racecxars"

Output: false

Explanation: Deleting the s at the end is not enough, because the x is still out of place. It takes two deletions to reach "racecar".

Check your understanding

Work out the answer for this input before you write any code.

s = "tacocats"

Constraints

  • 1 ≤ s.length ≤ 105
  • s holds lowercase English letters only.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.