DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Updated on

Algorithm 101: 6 Ways to Check if a Word is a Palindrome

If you followed my Algorithm 101 first episode, then this will be very easy. Here, the main objective is to reverse a given word and check if it still matches the given word.

wordPalindrome("Racecar"); // true

wordPalindrome("Race car"); // false
Enter fullscreen mode Exit fullscreen mode

In how many ways can you achieve this? I have 6 ways to Check if a given Word is a Palindrome. I know you will like to check them out.

This article will focus on word palindrome only. In a future episode, we will be looking at sentence palindrome.

Prerequisite

This article assumes that you have basic understanding of javascript's string and array methods.

Let's Check if a Word is a Palindrome using:

  • toLowerCase(), split(), reverse(), join(), if...statement;
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = lowerCasedWord
          .split("")
          .reverse()
          .join("");

        if (newWord === lowerCasedWord) {
          return true;
        }
        return false;
      }
Enter fullscreen mode Exit fullscreen mode
  • toLowerCase(), spread operator, reverse(), join(), if...statement;
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = [...lowerCasedWord].reverse().join("");

        if (newWord === lowerCasedWord) {
          return true;
        }
        return false;
      }
Enter fullscreen mode Exit fullscreen mode
  • toLowerCase(), spread operator, if...statement
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = [...lowerCasedWord].reduce((total, acc) => acc + total);

        if (newWord === lowerCasedWord) {
          return true;
        }
        return false;
      }
Enter fullscreen mode Exit fullscreen mode
  • toLowerCase(), for...loop, join(), if...statement;
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = [];

        for (let i = lowerCasedWord.length; i >= 0; i--) {
          newWord.push(lowerCasedWord[i]);
        }

        if (newWord.join("") === lowerCasedWord) {
          return true;
        }
        return false;
      }
Enter fullscreen mode Exit fullscreen mode
  • toLowerCase(), for...of...loop, if...statement;
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = "";

        for (char of lowerCasedWord) {
          newWord = char + newWord;
        }

        if (newWord === lowerCasedWord) {
          return true;
        }
        return false;
      }
Enter fullscreen mode Exit fullscreen mode
  • toLowerCase(), for...in...loop, if...statement;
      function wordPalindrome(word) {
        let lowerCasedWord = word.toLowerCase();
        let newWord = "";

        for (char in lowerCasedWord) {
          newWord = lowerCasedWord[char] + newWord;
        }

        if (newWord === lowerCasedWord) {
          return true;
        }
        return false;
      }
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are many ways to solve problems programmatically. You are only limited by your imagination. I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

Up Next: Algorithm 101: 3 Ways to Find Hamming Distance

You can also follow and message me on social media platforms.

Twitter | LinkedIn | Github

Thank You For Your Time.

Top comments (8)

Collapse
 
nkemjiks profile image
Mbonu Blessing

Nice one. I think just doing

return new word === lowerCasedWord

Should work for the if statement and return false line

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Thank you Blessing. I will definitely check that out

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

In fact, I just did and I love the simplicity

dev-to-uploads.s3.amazonaws.com/i/...

Collapse
 
chanting_baniya profile image
Aman • Edited

Good post. Really liked various ways you have noted. I will suggest one more. It should work too. Let me know...

Use two pointers starting from 0th and (string.length-1)th index, increase one and decrease the other respectively, till they meet at the center and compare characters at those pointers. If they match continue, else return false.

The advantage you gain is no excess space is required.

Collapse
 
leonardosnt profile image
Leonardo Santos • Edited

That is what I would do.
That approach is also useful to reverse arrays, just swap 0th <-> (len-1)th and so on.

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

I will definitely check it out. Sounds like divide-and-conquer. Lol

Collapse
 
benking_engr1 profile image
Ben-King Engineering Company

Good day sir. Please I have a challenge on how to derive an algorithm and draw a flowchart to determine if a word is a palindrome. And also I would like to use c++ program to express the algorithm

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

It's been a while I wrote C++. I don't even know if I can remember the syntax. Lol.

However, you can easily convert any of my solutions to C++ if you understand both JS and C++