Categories
algorithms kotlin

Problem 5.9 from EPI -> Is number a palindrome?

Given a number, check if it is a palindrome or not, for example 121 => true, 122 => false, 2 => true.
There is an easy way, convert the number to string then reverse the string, if both strings are equal it is a palindrome, however that solutions takes up more space, to solve this problem in O(1) space take a look at this solution here. It basically checks how many digits a number has, then repeatedly takes the left most right digit and the right most digit and compares the two, if they don’t match at any step, return false, otherwise return true.

Leave a Reply

Your email address will not be published. Required fields are marked *