-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid-palindrome.js
48 lines (39 loc) · 1.22 KB
/
valid-palindrome.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
43
44
45
46
47
48
/*
Valid Palindrome (https://leetcode.com/problems/valid-palindrome/)
- Given a string s, return true if it's a valid palindrome. Otherwise, return false
- Definition of valid palindrome:
- After ignoring all non-aphabumeric characters and case sensitivity, a string reads the same forward and backward
- An empty is considered as a valid palindrome
*/
// ---- Test Cases ----
const s1 = "A man, a plan, a canal: Panama"; // true
const s2 = "Race car"; // true
const s3 = "aabaa"; // true
const s4 = "abc"; // false
const s5 = "a"; // true
const s6 = ""; // true
// ---- Solution ----
const isPalindrome = function (s) {
// Ignore all non-alphanumeric characters and case sensitivity
s = s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
if (s.length < 2) return true;
let left = 0;
let right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
};
console.log(isPalindrome(s1)); // true
console.log(isPalindrome(s2)); // true
console.log(isPalindrome(s3)); // true
console.log(isPalindrome(s4)); // false
console.log(isPalindrome(s5)); // true
console.log(isPalindrome(s6)); // true
// ---- Space and Time Complexity ----
/*
Time: O(n)
Space: O(1)
*/