Palindrome Check

A corgi smiling happily

Write a function that takes in a non-empty string and that returns a boolean representing whether the string is a palindrome.

A palindrome is defined as a string that’s written the same forward and backward. Note that single-character strings are palindromes.

Sample Input

1string = "abcdcba"

Sample Output

1true // it's written the same forward and backward


Hint 1

Start by building the input string in reverse order and comparing this newly built string to the input string. Can you do this without using string concatenations?

Hint 2

Can you optimize your algorithm by using recursion? What are the implications of recursion on an algorithm’s space-time complexity analysis?

Hint 3

Go back to an iterative solution and try using pointers to solve this problem: start with a pointer at the first index of the string and a pointer at the final index of the string. What can you do from there?

Optimal Space & Time Complexity

O(n) time | O(1) space - where n is the length of the input string

1function isPalindrome(string) {
2 if (string.length === 1) return true;
3 if (string.length === 2) {
4 if (string[0] === string[1]) {
5 return true
6 } else return false
7 }
8 string.charAt[string.length]
9 if (string[0] === string[string.length - 1]) {
10 let sliced = string.slice(1, string.length - 1)
11 return isPalindrome(sliced);
12 }
13 return false
1function isPalindrome(string) {
2 let reversedString = '';
3 for (let i = string.length - 1; i >= 0; i--) {
4 reversedString += string[i];
5 }
6 return string === reversedString;


Do you have any questions, or simply wish to contact me privately? Don't hesitate to shoot me a DM on Twitter.

Have a wonderful day.
Abhishek 🙏

Subscribe to my newsletter

Get email from me about my ideas, full-stack development resources, tricks and tips as well as exclusive previews of upcoming articles.

No spam. Just the highest quality ideas you’ll find on the web.