Skip to content

Commit 4372706

Browse files
committed
Valid Palindrome solution
1 parent 1c502dd commit 4372706

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

valid-palindrome/kimyoung.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var isPalindrome = function (s) {
2+
let left = 0, right = s.length - 1;
3+
while (left < right) {
4+
while (left < right && !isAlphaNumeric(s[left])) { // increment the left index until it's an alphanumeric character
5+
left++;
6+
}
7+
while (left < right && !isAlphaNumeric(s[right])) { // decrement the right index until it's an alphanumeric character
8+
right--;
9+
}
10+
if (s[left++].toLowerCase() !== s[right--].toLowerCase()) return false; // compare the two string values, if different return false;
11+
}
12+
return true;
13+
};
14+
15+
function isAlphaNumeric(char) { // use ASCII code to findout if char is alphanumeric or not
16+
const asciiCode = char.charCodeAt(0);
17+
return (asciiCode >= 65 && asciiCode <= 90) ||
18+
(asciiCode >= 97 && asciiCode <= 122) ||
19+
(asciiCode >= 48 && asciiCode <= 57)
20+
}
21+
22+
// time - O(n) iterate through the string once
23+
// space - O(1) no extra space created

0 commit comments

Comments
 (0)