Overview
Implementing algorithms to detect Palindromes is by far one of the most popular in the field of programming. If you are reading this article, the tendency that you already understand what palindromes are is high but to our friends that have not gotten a grip of what palindromes really are, An integer is a palindrome when it reads the same backward as forward. For example, 121 is a palindrome while 123 is not. 121 reads as 121 from left to right and from right to left. From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Having understood the concept of Palindromes, let us look at how we can write a code that can help us determine if a number is a palindrome or not.
Thought Process
Think about this for a while, how do I know if a number reads from left to right is the same as when read from right to left, you simply check it physically, in the same way, to implement this function we simply check if the number is the same with the reverse version of it. There are quite a number of ways to implement palindrome, one is to check the numbers one by one both from the front and also from the back, and another way is to first reverse the number and then check if it is the same as the original. I used the latter method in this task, given an integer x, the first thing is to convert x to string so it can be easily reversed,
string s = to_string(x);
r == string(s.rbegin(), s.rend());
and then followed by reversing x and saving the reversed version in a separate variable. We can now check if our original integer is the same as the reversed version and with that, we have our palindrome checker.
Conclusion
Depending on how you can think about it, Palindromes detectors are useful software to have handy and it can be used as a form of practice of programming skills. Time complexity : O(log(n)) Space complexity: O(1)