Skip to content
JsDevLife
Menu
  • Home
  • Leetcode in JS
  • About me
  • Contact Us
Menu

JavaScript find valid palindrome or not | leet code question 125

Posted on July 2, 2022 by Vikas Kad

Hello friends, 

 If you come here to check the solution of the following question. 

 JavaScript find valid palindrome or not

this question is asked in LeetCode as a valid palindrome(Question Number -125) and its
difficulty is easy.

If you search on google mostly we get solutions in Java, Python, or C++. if we are lucky then only we get the
solution in javascript for the leet code questions.
So let’s first understand the question.


A phrase is a palindrome if it reads
the same forward and backward after converting all uppercase letters into lowercase letters and removing all
non-alphanumeric characters. Alphanumeric characters include letters and numbers.



Given a string s,
return true if it is a palindrome, or false otherwise.



Example 1:

Input: s = “A man, a plan, a canal: Panama”
Output: true
Explanation: “amanaplanacanalpanama” is a palindrome.


Example 2:

Input: s = “race a car”
Output: false
Explanation: “raceacar” is not a palindrome.


Example 3:

Input: s = ” ”
Output: true
Explanation: s is an empty string “” after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.


 


Constraints:

  • 1
    <= s.length <= 2 * 105
  • s consists
    only of printable ASCII characters.


So the solution to the above question is.





/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function(s) {

    s = s.toLowerCase().replace(/[W_]/g,""); 	// replace() method will replace all non alphanumeric to empty chars
    let left = 0;
    let right = s.length-1;
    while(left < right){
        if(s[left]!==s[right]){
            return false;
        }
        left++;
        right--;
        
    }
    return true;
    
};


Complexity of the solution is as follows:

Time Complexity: O(N)

Space Complexity: O(1)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Solving LeetCode Problem 79 – Word Search in JavaScript
  • Solving LeetCode Problem 48: Rotate Image using JavaScript
  • Solving the “Container With Most Water” LeetCode Problem in JavaScript – A Comprehensive Guide
  • LeetCode Solution: 54: Spiral Matrix in JavaScript
  • Solution to LeetCode Problem 31. Next Permutation in JavaScript

Archives

  • 2023 (5)
  • 2022 (20)
  • 2021 (2)
  • 2020 (4)
  • 2019 (14)
  • 2018 (17)

Categories

  • blockchain development
  • Blog
  • crystal
  • flutter
  • flutter.io
  • GitHub
  • Installation
  • Ionic Framework
  • javascript
  • leetcode-in-js
  • masteringInJavasript
  • mcqs
  • MongoDB
  • nodejs
  • Object Oriented Javacript
  • python
  • smart contracts
  • visual studio

Quick Links

  • Home
  • Leetcode in JS
  • About me
  • Contact Us

Terms of service

  • Terms Of Service
  • Disclaimer
©2023 JsDevLife | Design: Newspaperly WordPress Theme