DEV Community

Mahdi Shamlou | Solving LeetCode #9: Palindrome Number — Step by Step

Hey everyone! Mahdi Shamlou here — back with another LeetCode classic problem 🚀

After #8 String to Integer (atoi), today I solved Problem #9 — Palindrome Number.

Mahdi Shamlou

At first, I thought:

"This should be easy... just check whether the number reads the same from left to right and right to left."

But instead of reversing the number, I decided to treat it like a palindrome string problem and expand from the center.


Problem Statement

Given an integer x, return true if x is a palindrome, and false otherwise.

A palindrome number reads the same backward as forward.

Examples

Input: 121
Output: true
Enter fullscreen mode Exit fullscreen mode
Input: -121
Output: false
Enter fullscreen mode Exit fullscreen mode
Input: 10
Output: false
Enter fullscreen mode Exit fullscreen mode
Input: 1221
Output: true
Enter fullscreen mode Exit fullscreen mode

My Thought Process

When I started, this was my idea:

  1. Change x from integer to string.
  2. Find the center of the string.
  3. Expand around the center.
  4. Compare characters on both sides.
  5. If everything matches until the edges, return True.

My initial notes were literally:

  • first of all i think change x from int to str
  • secend i think i can find center of str and expand around the center with consider odd or even x_str
  • three if any one of them is not equal we can return False else return True

So that's exactly the approach I implemented.


My Solution

class Solution:
    def isPalindrome(self, x: int) -> bool:
        """
            first of all i think change x from int to str 
            secend i think i can find center of str and expand around the center with consider odd or even x_str
            three if any one of them is not equal we can return False else return True 
        """
        x_str = str(x)
        x_is_palindrome = False   

        print(f"x : {x_str} and type : {type(x_str)}")

        if len(x_str) % 2 == 0:

            print(f"x is even and len is : {len(x_str)}")
            mid_of_str_num = len(x_str) / 2

            left = mid_of_str_num - 1
            right = mid_of_str_num 
            print(f"left for start is : {left}")
            print(f"right for start is : {right}")

            while left >= 0 and right < len(x_str) and x_str[int(left)] == x_str[int(right)]:
                left -= 1
                right += 1

            left = left + 1 
            right = right - 1

            print(f"left in end is : {left}")
            print(f"right in end is : {right}")

            return (left == 0) and (right == len(x_str)-1) 

        else:

            print(f"x is odd and len is : {len(x_str)}")
            mid_of_str_num = int(len(x_str) / 2)

            left = mid_of_str_num
            right = mid_of_str_num 

            print(f"left for start is : {left}")
            print(f"right for start is : {right}")

            while left >= 0 and right < len(x_str) and x_str[int(left)] == x_str[int(right)]:
                left -= 1
                right += 1

            left = left + 1 
            right = right - 1

            print(f"left in end is : {left}")
            print(f"right in end is : {right}")   

            return (left == 0) and (right == len(x_str)-1)      

        return False


x = Solution()
res = x.isPalindrome(x=121)
print(res)
Enter fullscreen mode Exit fullscreen mode

How It Works

For Odd Length Numbers

Example:

121
Enter fullscreen mode Exit fullscreen mode

The center is:

  2
Enter fullscreen mode Exit fullscreen mode

Start with:

left = center
right = center
Enter fullscreen mode Exit fullscreen mode

Then expand outward:

1 2 1
^   ^
Enter fullscreen mode Exit fullscreen mode

If both sides keep matching until we reach the edges, the number is a palindrome.


For Even Length Numbers

Example:

1221
Enter fullscreen mode Exit fullscreen mode

There is no single center.

Instead we start from:

12|21
  ^
Enter fullscreen mode Exit fullscreen mode

and compare:

1 2 2 1
  ^ ^
Enter fullscreen mode Exit fullscreen mode

Then expand:

1 2 2 1
^     ^
Enter fullscreen mode Exit fullscreen mode

If every comparison matches, we return True.


What I Learned

This problem reminded me of a common pattern:

Expand Around Center

I usually think about this technique for string palindrome problems, but it also works nicely here after converting the integer into a string.

Another thing I noticed is that handling odd-length and even-length cases separately makes the logic easier to understand.


Complexity Analysis

Time Complexity

O(n)
Enter fullscreen mode Exit fullscreen mode

We may visit each character once while expanding from the center.

Space Complexity

O(n)
Enter fullscreen mode Exit fullscreen mode

Because we convert the integer into a string.


Final Thoughts

This problem wasn't really about a complicated algorithm.

For me, it was about taking a familiar palindrome technique and applying it to numbers.

I know there are other solutions that avoid converting to a string, but I wanted to solve it using the first idea that came to my mind and make it work correctly.

And that's one thing I enjoy about LeetCode:

Sometimes the best solution is not the first accepted solution. But every accepted solution teaches you something.


What About You?

Did you solve this problem using:

  • String conversion?
  • Reversing the integer?
  • Two pointers?
  • Another approach?

Drop your thoughts — I read everything! 🚀

My Repo (All Solutions)

GitHub — mahdi0shamlou/LeetCode


Want More?

Mahdi Shamlou

If you enjoyed this deep dive, check out my other articles:


🔗 LinkedIn: https://www.linkedin.com/in/mahdi-shamlou-3b52b8278
📱 Telegram: https://telegram.me/mahdi0shamlou
📸 Instagram: https://www.instagram.com/mahdi0shamlou/

Author: Mahdi Shamlou | مهدی شاملو

Top comments (0)