Meta VO 面试真题解析:Valid Palindrome II(最多删除 1 个字符)

16次阅读
没有评论

Given a string s consisting of lowercase English characters, determine if you can make it a palindrome by removing at most one letter.

Example 1:

Input: s = "aba"
Output: true

Example 2:

Input: s = "abca"
Output: true

Example 3:

Input: s = "cupcua"
Output: true

Example 4:

Input: s = "abcde"
Output: false

这道题考察的是双指针判断回文的经典变体:先从字符串两端向中间收缩,一旦遇到不相等的位置,就分别尝试跳过左边或右边一个字符,再判断剩余部分是否仍然是回文。因为只允许删除最多 1 个字符,所以整体只需线性扫描,时间复杂度为 O(n),额外空间为 O(1)。

正文完
 0