Leetcode 680. Valid Palindrome II

Poby’s Home
Aug 10, 2021

--

easy

Example 1:

Input: s = “aba”
Output: true
Example 2:

Input: s = “abca”
Output: true
Explanation: You could delete the character ‘c’.
Example 3:

Input: s = “abc”
Output: false

class Solution {
public:
bool sub(int i, int j, string& s) {
while(j>i) {
if (s[i] == s[j]) {
i++;
j--;
} else {
return false;
}
}
return true;
}

bool validPalindrome(string s) {
int i=0;
int j=s.size() - 1;
while (j>=i) {
if (s[i] == s[j]) {
i++;
j--;
continue;
} else {
return sub(i+1, j, s) || sub(i, j-1, s);
}
}

return true;
}
};

--

--

No responses yet