Skip to content

Instantly share code, notes, and snippets.

@sAVItar02
Created November 5, 2024 02:57
Show Gist options
  • Save sAVItar02/ebf6707c3404cb61fe2230a9c04d5de0 to your computer and use it in GitHub Desktop.
Save sAVItar02/ebf6707c3404cb61fe2230a9c04d5de0 to your computer and use it in GitHub Desktop.
Valid Palindrome
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
const regex = /[^a-z0-9]/gmi;
s = s.replace(regex, "").toLowerCase();
let l = 0;
let r = s.length - 1;
while(l <= r) {
if(s[l] === s[r]) {
l++;
r--;
} else {
return false;
}
}
return true;
};
// Convert the given string to desired form using regex and replace
// Run a while loop with 2 pointers, one pointing at the start and the other at the end of the string
// If the value at both pointers is same, increase the start pointer and decrease the end pointer till they meet at the middle
// If at any place the values are not same, return false
// Else return true
// Time: O(n)
// Space: O(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment