Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
Valid Palindrome - compare against reverse
const string = "A man, a plan, a canal: Panama"
const isValidPalindrome = function(s) {
s = s.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
let rev = "";
// generate a reverse string using a reverse for loop.
for(let i = s.length - 1; i >= 0; i--) {
rev += s[i];
}
return rev === s;
};
console.log(isValidPalindrome(string));
Valid Palindrome - 2 pointers from outside
const string = "A man, a plan, a canal: Panama"
const isValidPalindrome = function(s) {
s = s.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
// initialize left/right pointers at start and end of string respectively
let left = 0; right = s.length - 1;
// loop through string characters while comparing them, then move the pointers closer to the center
while(left < right) {
if(s[left] !== s[right]) {
return false
}
left++;
right--;
}
return true;
};
console.log(isValidPalindrome(string));
Valid Palindrome - 2 pointers from center
rom const string = "A man, a plan, a canal: Panama"
const isValidPalindrome = function(s) {
s = s.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
// initialize left/right pointers to point at the middle index of the string. Remember, indexes start at 0 meaning that we have to floor() the value from dividing length by 2 in order to get the index of the center.
let left = Math.floor(s.length / 2), right = left;
// if the string is even, move left pointer back by 1 so left/right are pointing at the 2 middle values respectively.
if(s.length % 2 === 0) {
left--;
}
// loop through the string while expanding pointers outwards comparing the characters.
while(left >= 0 && right < s.length) {
if(s[left] !== s[right]) {
return false
}
left--;
right++;
}
return true;
};
console.log(isValidPalindrome(string));
Almost palindrome
Given a string s, return true if the s can be palindrome after deleting at most one character from it.
var validPalindrome = function(s) {
let start = 0;
let end = s.length - 1;
while (start < end) {
if (s[start] !== s[end]) {
return validSubPalindrome(s, start + 1, end) || validSubPalindrome(s, start, end - 1);
}
start++;
end--;
}
return true;
};
var validSubPalindrome = function(s, start, end) {
while (start < end) {
if (s[start] !== s[end]) {
return false;
}
start++;
end--;
}
return true;
};
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team