[LeetCode] Valid Palindrome II

680. Valid Palindrome II

Given a string s, return true if the s can be palindrome after deleting at most one character from it.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
bool helper(string& s, bool rm, int l, int r) {
if(l >= r) return true;
if(s[l] == s[r]) return helper(s,rm,l+1,r-1);
if(rm) return false;
return helper(s,true,l+1,r) or helper(s,true,l,r-1);
}
public:
bool validPalindrome(string s) {
return helper(s,false,0,s.length()-1);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/02/PS/LeetCode/valid-palindrome-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.