Check whether a String is Palindrome or not
Problem
Return true if the string reads the same forwards and backwards.
Example 1
Input: "level" Output: true
Example 2
Input: "hello" Output: false
Test cases
Case 1
Input: "level" Expected: true
Case 2
Input: "hello" Expected: false
Case 3
Input: "abba" Expected: true
Case 4
Input: "a" Expected: true
Case 5
Input: "" Expected: true
View solutionHide solution2 approaches
1. Two pointers
Time O(n)Space O(1)
Compare characters from both ends moving inward.
function isPalindrome(s) { let l = 0, r = s.length - 1; while (l < r) if (s[l++] !== s[r--]) return false; return true;}2. Reverse and compare
Time O(n)Space O(n)
One-liner; O(n) space for the reversed copy. Fine to mention, not the answer they want.
const isPalindrome = (s) => s === [...s].reverse().join('');