Valid Palindrome

Robert M Ricci
The Startup
Published in
4 min readFeb 21, 2021

Using Ruby

Photo by Cassie Matias on Unsplash

So in this post, I am going to discuss how to tell if a string or phrase is a palindrome. Now you may be asking what is a palindrome, why do I need to know if a string is one. Well, those are both very good questions. In answer to the first, a palindrome is a word or phrase that is the same forward as reversed. My favorite example of this would have to be “tacocat”(because I love tacos). Now as to why do you need to be able to know why a string or phrase is a palindrome, and that's because it is a popular technical interview question.

Now checking if a word is a palindrome is relatively easy, where things become more complicated is when you have to check a phrase. The difficulty lies in the punctuation and white spaces both things we don't care about. For example, the leetcode test is “A man, a plan, a canal: Panama". So, first I’m going to just solve the problem for a word, then I will add in the functionality for a phrase.

STRING

So we will start with a function named is_palidrome, which accepts a string.

def is_palindrome(str)
end

Then we will need a conditional to check and see if the string is the same forward and backward. We will use a ruby method to do that.

def is_palindrome(str)     if str != str.reverse
return false
else
return true
end
end

Well, jobs done. That’s it pack it up, lets get out of here. What? You want to know what happens if the word is capitalized because it would still be a palindrome. That’s a very good point, hmmmm, I think I know how to account for that. We would either need to lowercase or uppercase all the letters. Like the example below.

def is_palindrome(str)     if str.downcase != str.downcase.reverse
return false
else
return true
end
end

This would make the word all lowercase on both sides and then check if the word is the same forward as it is reversed.

PHRASE

Now we will go over how to work with a phrase. For this, we will have to work with the dreaded Regex(dun dun dun), also known as regular expressions. These handy little beasts are going to help us filter out punctuation, and white spaces.

def is_palindrome(str)
str = str.gsub(/[^a-zA-Z0-9]/,"")
if str.downcase != str.downcase.reverse
return false
else
return true
end
end

We use .gsub which is a ruby method that returns a copy with all occurrences of pattern substituted for the second argument. What that expression is saying is that we are going to remove anything that is not a upper or lowercase letter or a number. So for example out test phrase would be:

// “A man, a plan, a canal: Panama"      //would be come// AmanaplanacanalPanama

You will notice that the “A” and “P” are still capilatlized. That’s because the regex isn’t doing anything with them. We are using downcase(or upcase) to take care of that particular problem.

BONUS LESSON: PALINDROME NUMBER

I decided to add on the this is just as a bonus. For the palidrome number, the solution is going to be a little different. For starters you won’t have to use regex, but we will still have to use some Ruby built-in methods. So lets take a look.

We will start with a method called is_palidrome, that accepts an integer. We are going to use a conditional just like we did for the one with the string. After that things are going to be a little different. The easiest was to test if a number is a palindrome would be to turn it into a string and the reverse it just like we did with the previous one. In case you are wondering why we can’t just reverse the interger, it’s becsue reverse doesn't work on a number.

def is_palindrome(integer)
if integer < 0 || integer != integer.to_s.reverse.to_i
return false
else
return true
end
end

The first thing we check is that the number is positive. If it’s negative, it can’t be a palidrome because it wont’ have a negative on the other side. The next thing we check is if the integer reversed is the same as the integer forward. If either of these fail the interger is not a palindrome. We convert one to a string, then we reverse it, then back to a integer for comparison.

CONCLUSION

If I’m being honest these aren’t the fastest ways of solving these problems. Think of it like a steping stone. It will get you in the right direction, just as it did me while I was writing it. I hope this was helpful. Please, let me know what you think. I will post some resources for anyone whos looking for some.

RESOURCES

--

--

Robert M Ricci
The Startup

Full Stack Developer Ruby and Javascript. Recent grad of the Flatiron School.