Palindrome Learning Recap

Toni T Diep
2 min readNov 19, 2021

--

Learning palindrome on a birthday week is not the business. In any birthday season of practicing DSA, I am committed to understanding the actual time and space complexity — so here we go! #postBootcampThings

Palindrome Bob
A mirror is my visual analogy to Palindrome. https://media.giphy.com/media/3oEhn1BB9DdrWU0KfC/giphy.gif

Understanding Palindrome with JavaScript and Regex. What is the Palindrome? — this took me still a moment to understand what is moving backward and forward. According to Wikipedia’s definition of “palindrome”: Characters, words, or lines — The characters read the same backward as forward. Some examples of palindromic words are redivider, deified, civic, radar, level, rotor, kayak, reviver, racecar, madam, and refer. These bolded words examples, “ci-v-ic” of “civic” is a palindrome, demonstrated with the“odd character” appearing once while the remaining letters are mirrors at the front and back or left and right of that singled-used-odd character. Now let’s see how this is performed using JavaScript and Regex to check for palindrome.

JavaScript built-in method of .replace() with Regex to verify the case-sensitive palindrome to remove all non-alphanumeric characters and convert all uppercase letters into lowercase letters, as it should read completely the same way both forward and backward. Valid alphanumeric characters include letters and numbers.

Points to consider for Leetcode #125 Valid Palindrome:

  • when checking for palindrome in the given string of “s” we are also dealing with a boolean of true if the verified results indeed is a palindrome. Otherwise, return false.
  • Time complexity is O(n) with ’n’ in regards to the length of the given string at s = “A man, a plan, a canal: Panama”
  • Space complexity is O(n) with ’n’ fails the verify the string, ‘s’, palindrome checks.
var isPalindrome = function(s) {

var removeChar = s.replace(/[^A-Z0-9]/ig, "").toLowerCase();
var verifyPalindrome = removeChar.split('').reverse().join(''); return removeChar === verifyPalindrome;};

If you like it, clap it. Please share all the ways to solve this one. Thank you for reading.

Resources

Happy coding y’all.

--

--

Toni T Diep
Toni T Diep

Written by Toni T Diep

multilingual Software Engineer, always learning and growing.

No responses yet