1
/*
2
Solution:
3

4
1. Use two pointers, one initialised to 0 and the other initialised to end of string. Check if characters at each index
5
are the same. If they are the same, shrink both pointers. Else, we have two possibilities: one that neglects character
6
at left pointer and the other that neglects character at right pointer. Hence, we check if s[low+1...right] is a palindrome
7
or s[low...right-1] is a palindrome. If one of them is a palindrome, we know that we can form a palindrome with one deletion and return true. Else, we require more than one deletion, and hence we return false.
8
*/
9
var validPalindrome = function (s) {
10
let low = 0,
11
high = s.length - 1;
12
while (low < high) {
13
if (s[low] !== s[high]) {
14
return isPalindrome(s, low + 1, high) || isPalindrome(s, low, high - 1);
15
}
16
low++, high--;
17
}
18
return true;
19
// T.C: O(N)
20
// S.C: O(1)
21
};
22

23
function isPalindrome(str, low, high) {
24
while (low < high) {
25
if (str[low] !== str[high]) return false;
26
low++, high--;
27
}
28
return true;
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0