Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /0009 - Palindrome Number
    0009 - Palindrome Number
    0009 - Palindrome Number
    0009 - Palindrome Number

    0009 - Palindrome Number

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/palindrome-number/

    Solution 1 - String Conversion

    Convert the integer to a string and check if it reads the same forwards and backwards. Negative numbers are never palindromes.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)

    Solution 2 - Reverse Half of the Number

    Instead of converting to a string, reverse only the second half of the number and compare it with the first half. This avoids overflow issues and uses no extra string space.

    TimeComplexity:O(log⁡10(n))TimeComplexity: O(\log_{10}(n))TimeComplexity:O(log10​(n))
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0009.1 - Palindrome Number - Solution 1 - String Conversion """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def isPalindrome(self, x: int) -> bool:
            """Palindrome Number Function"""
            return str(x) == str(x)[::-1]
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().isPalindrome(121))       # True
        print(Solution().isPalindrome(-121))      # False
        print(Solution().isPalindrome(10))        # False
        print(Solution().isPalindrome(0))         # True
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0009.2 - Palindrome Number - Solution 2 - Reverse Half """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def isPalindrome(self, x: int) -> bool:
            """Palindrome Number Function"""
            # Negative numbers and numbers ending in 0 (except 0 itself) are not palindromes
            if x < 0 or (x % 10 == 0 and x != 0):
                return False
    
            reversed_half = 0
            while x > reversed_half:
                reversed_half = reversed_half * 10 + x % 10
                x //= 10
    
            # For even-length numbers: x == reversed_half
            # For odd-length numbers: x == reversed_half // 10 (drop the middle digit)
            return x == reversed_half or x == reversed_half // 10
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().isPalindrome(121))       # True
        print(Solution().isPalindrome(-121))      # False
        print(Solution().isPalindrome(10))        # False
        print(Solution().isPalindrome(0))         # True
        print(Solution().isPalindrome(12321))     # True
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()