-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisPalindrome.js
More file actions
executable file
·42 lines (34 loc) · 1.37 KB
/
isPalindrome.js
File metadata and controls
executable file
·42 lines (34 loc) · 1.37 KB
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
// function to check if a given string is a palindrome, ignoring alphanumeric characters and case sensitivity
// what is palindrome?
// A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization
// Example:
// Input: "A man, a plan, a canal: Panama"
// Output: true
// Explanation: "amanaplanacanalpanama" is a palindrome.
function isPalindrome(s) {
let left = 0;
let right = s.length - 1;
while (left < right) {
if (!isAlphaNum(s[left])) left++;
else if (!isAlphaNum(s[right])) right--;
else {
if (s[left].toLowerCase() !== s[right].toLowerCase()) return false;
left++;
right--;
}
}
return true;
}
function isAlphaNum(ch) {
return /[a-z0-9]/i.test(ch);
}
// test the function
console.log(isPalindrome("A man, a plan, a canal: Panama")); // Output: true
console.log(isPalindrome("racecar")); // Output: true
console.log(isPalindrome("hello")); // Output: false
console.log(isPalindrome("No 'x' in Nixon")); // Output: true
console.log(isPalindrome("12321")); // Output: true
console.log(isPalindrome("12345")); // Output: false
console.log(isPalindrome("Able was I ere I saw Elba")); // Output: true
console.log(isPalindrome("Madam, in Eden, I'm Adam")); // Output: true
console.log(isPalindrome("This is not a palindrome")); // Output: false