680.Valid Palindrome II
Example 1:
Input: "aba"
Output: True
Example 2:
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
Note:
The string will only contain lowercase characters a-z. The maximum length of the string is 50000.class Solution {
bool isValid(int left,int right,string s)
{
while(left<right)
{
if(s[left++] != s[right--])
return false;
}
return true;
}
public:
bool validPalindrome(string s) {
if(s.length()<3) return true;
int left=0;
int right=s.length()-1;
while(left<right)
{
if(s[left]!=s[right]) return isValid(left+1,right,s) || isValid(left,right-1,s);
left++;
right--;
}
return true;
}
};Last updated