Algorithms and Data Structures Part II

Palindromes and Integer Reversal

Brian Butterly
5 min readFeb 12, 2021

Okay, so if you’re here before reading part one head on over HERE and give that a read before continuing on, I’ll wait…

Alright, here we go!

Palindromes

For this problem you are given a string and asked to return true if the string is a palindrome or false if it is not. Palindromes are strings that form the same word forwards or backwards you can see examples in the picture at the top of this post.

So once again, as in the previous post, we will start with a function that takes in an argument of str.

First thing we need to do is reverse the string just like we did in my previous post, using our reverse function. Don’t forget, this is a string so we need to turn it into an array, reverse it then turn it back into a string…

As you can see I created a variable called reversed and I used the previous function of calling .split(“”).reverse().join(“”) on the str argument. So I split the string into an array, reversed the array, then joined it back into a string. Now I have my reversed string all I have to do is compare it to the str argument and if they match it’s a palindrome so it’s true. If not it’s false, like so…

And that’s it. This is the most straightforward solution to this type of problem.

Part 2 of this post will look at reversing an integer which will require a little extra logic.

Integer Reversal

For this problem you are given an integer and asked to return an integer that is the reverse order of numbers…

e.g. 15 === 51 || 981 ===189 || -15 === -51 || 500 === 5

notice we don’t want 500 to equal 005 or -15 to be 51- that will be important in a little bit.

So we know how to reverse a string by now, but we are working with numbers. That’s ok, we have a way to turn a number into a string using the toString function…

const myNumber = 200
myNumber.toString()

This will now give us a string to work with.

Another trick we can use is the Math.sign() function. What we can do with this function is pass in a number, if that number is positive it will return 1 if it is negative it will return -1…

Math.sign(2000)  //returns 1
Math.sign(-2000) //returns -1

So this may be helpful with returning the - sign in the actual number.

The last little trick we may need is the parseInt() function. This takes a string and turns it into what it thinks may be a number.

const myNumber = 20myNumber.tostring()

this is a string of ‘20’

const myNumber = 20parseInt(myNumber.tostring())

now we are dealing with a number again.

Now let's get to the solution…

We have our function reverseInt(n) n is the number that gets provided to us so we need to turn n into a string.

n.toString()

we are now dealing with a string, so we can do the usual .split(“”).reverse().join(“”)

n.toString().split('').reverse().join('');

but this will give us a problem, it will return -15 as 51-, it just reversed everything including the placement of the -. This is also still a string. That’s not what we want, we want -15 to be -51 as a number.

One thing we can do is make this a variable like so…

const reversed = n.toString().split('').reverse().join('');

Then return parseInt…

return parseInt(reversed);

This gives us a number back.

51

but the negative sign is gone. The parseInt() function has somehow lost the negative sign. So we need to maybe say…

if (n < 0) {
return parseInt(reversed) * -1;
}
return parseInt(reversed)
}

this says if the n argument is less than 0 add a negative sign by multiplying by -1, if not leave it alone. This will definitely get us our answer but having two return statements may not be ideal so we can incorporate one of those trick’s I mentioned earlier Math.sign()

All we do is return our parseInt(reversed) then multiply Math.sign(n) so Math.sign is taking in the number argument and if it is negative it will multiply by -1 (adding the -) if it is positive it will multiply by 1 (returning a number without a negative).

So given -5 it will return -5 still, given -51 it will return -15 given 23 we will get 32 and so on.

This is a somewhat complicated problem but once you know the trick’s it becomes quite easy. Next week, in part III I will discuss a technique that is helpful in a lot of other string related problems so please look out for it as it will be a hopefully useful read.

Thanks for reading.

--

--