Skip to content

Commit 7e447b5

Browse files
committed
Added validPalindrome solution
1 parent bb00712 commit 7e447b5

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

valid-palindrome/nhistory.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function (s) {
6+
// 1. Create left and right pointer
7+
let l = 0;
8+
let r = s.length - 1;
9+
// 2. Iterate while loop with l<r condition
10+
while (l < r) {
11+
// 3. Check character with left pointer is alphanumeric character or not
12+
if (
13+
(s[l] >= "a" && s[l] <= "z") ||
14+
(s[l] >= "A" && s[l] <= "Z") ||
15+
(s[l] >= "0" && s[l] <= "9")
16+
) {
17+
// 4. Check character with right pointer is alphanumeric character or not
18+
if (
19+
(s[r] >= "a" && s[r] <= "z") ||
20+
(s[r] >= "A" && s[r] <= "Z") ||
21+
(s[r] >= "0" && s[r] <= "9")
22+
) {
23+
// 5. Compare left and right pointer character
24+
if (s[l].toLowerCase() !== s[r].toLowerCase()) {
25+
return false;
26+
} else {
27+
l++;
28+
r--;
29+
}
30+
// If not, go to next location for right pointer
31+
} else r--;
32+
33+
// If not, go to next location for left pointer
34+
} else l++;
35+
}
36+
return true;
37+
};
38+
39+
// TC: O(n)
40+
// SC: O(n)
41+
42+
console.log(isPalindrome("A man, a plan, a canal: Panama")); //true
43+
console.log(isPalindrome("race a car")); //false

0 commit comments

Comments
 (0)