-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0680_validPalindromeII.js
More file actions
34 lines (28 loc) · 914 Bytes
/
0680_validPalindromeII.js
File metadata and controls
34 lines (28 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @param {string} word Input string to check.
* @return {boolean} Can string be a palindrome with at least one character deleted.
* @summary Valid Palindrome II {@link https://leetcode.com/problems/valid-palindrome-ii/}
* @description Given a string, can it be a palindrome with at most one character removed.
* Space O(1) - no extra memory used.
* Time O(n) - iterate once over a list, perform up to two check (each is n).
*/
const validPalindrome = word => {
const isPalindrome = (word, left, right) => {
while (left < right) {
if (word[left] !== word[right]) return false;
left++;
right--;
}
return true;
};
let left = 0;
let right = word.length - 1;
while (left < right) {
if (word[left] !== word[right]) {
return isPalindrome(word, left + 1, right) || isPalindrome(word, left, right - 1);
}
left++;
right--;
}
return true;
};