-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0125_validPalindrome.js
42 lines (34 loc) · 1.22 KB
/
0125_validPalindrome.js
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
35
36
37
38
39
40
41
42
/**
* @param {string} s
* @return {boolean}
*/
/**
* @param {strng} s string to check if palindrome
* @return {boolean} True if string is a palindrome (only counting alphanumeric chars).
* @summary Valid palindrome {@link https://leetcode.com/problems/valid-palindrome/}
* @description Given an input string, return true if its a palindrome (non-alphanumeric chars excluded);
* Space O(1) - No extra space (few variables).
* Time O(n) - Traverse over the input string.
*/
const isPalindrome = s => {
if (!s) return true;
const isAlphanumeric = c =>
(c >= 'a'.charCodeAt() && c <= 'z'.charCodeAt()) || (c >= '0'.charCodeAt() && c <= '9'.charCodeAt());
let left = 0;
let right = s.length - 1;
const mid = Math.ceil(s.length / 2);
while (left <= right) {
const leftChar = s[left].toLowerCase();
const rightChar = s[right].toLowerCase();
const isLeftValid = isAlphanumeric(leftChar.charCodeAt());
const isRightValid = isAlphanumeric(rightChar.charCodeAt());
if (isLeftValid && isRightValid && leftChar !== rightChar) return false;
if (!isLeftValid && isRightValid) left++;
else if (isLeftValid && !isRightValid) right--;
else {
left++;
right--;
}
}
return true;
};