Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»Data Structures & Algorithms»Palindrome Number Program in Python | Leetcode #9
    Data Structures & Algorithms

    Palindrome Number Program in Python | Leetcode #9

    codeanddebugBy codeanddebug28 May 2025No Comments4 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Hello everyone! In this article, we’ll guide you through a thorough explanation of Leetcode #9. Explaining the palindrome number program in Python. The [Problem Link] is given here for your quick reference.

    Examples of Palindrome Number Program in Python:

    Example 1:
    
      Input: x = 121
      Output: true
      Explanation: 121 reads as 121 from left to right and from right to left.
    
    Example 2:
    
      Input: x = -121
      Output: false
      Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
    
    Example 3:
    
      Input: x = 10
      Output: false
      Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

    1. Optimal Solution

    Problem Statement:

    Objective: The given code aims to determine whether a given integer “x” is a palindrome. A palindrome is a number that reads the same backward as forward.

    Purpose: The function “isPalindrome” is designed to check if the integer “x” remains the same when its digits are reversed.

    Expected Input and Output:

    • Input: An integer “x”.
    • Output: A boolean value True if “x” is a palindrome, and False otherwise.

    Intuition and Approach:

    Intuition: To check if a number is a palindrome, we can reverse its digits and then compare the reversed number with the original number. If they are equal, the number is a palindrome.

    Approach:

    1. Handle negative numbers separately, as they cannot be palindromes
    2. Convert the number to its absolute value for easier processing (although the check for negativity at the beginning makes this unnecessary in the given code)
    3. Initialize a variable to store the reversed number
    4. Use a loop to extract the last digit of the number and build the reversed number
    5. Compare the reversed number with the original number to determine if it is a palindrome

    Code:

    class Solution:
        def isPalindrome(self, x: int) -> bool:
            if x < 0:
                return False
            num = x
            palindrome_number = 0
            while num > 0:
                last_digit = num % 10
                palindrome_number = (palindrome_number * 10) + last_digit
                num //= 10
            return palindrome_number == x
    1. Negative Number Check: The function immediately returns False if “x” is negative, as negative numbers cannot be palindromes.
    2. Initialize Variables: The variable “num” is initialized to “x” and “palindrome_number” is initialized to 0 to build the reversed number.
    3. Loop to Reverse Digits:
      1. While “num” is greater than 0, the last digit is extracted using num % 10.
      2. This digit is appended to “palindrome_number” by multiplying palindrome_number by 10 and adding the digit.
      3. The last digit is removed from “num” using integer division by 10.
    4. Return Comparison: After the loop, the function returns True if “palindrome_number” is equal to “x”, and False otherwise.

    Dry Run:

    Let’s walk through a step-by-step execution with a sample input:

    Also read about Leetcode #7 : Reverse Integer Python Program.

    Potential Edge Cases:

    1. Negative Numbers:
      • Negative numbers should immediately return False.
    2. Single-Digit Numbers:
      • Any single-digit number should return True as it is inherently a palindrome (e.g., x = 5).
    3. Zero:
      • x = 0 should return True as it reads the same forward and backward.

    Handling Edge Cases:

    • The given code handles negative numbers and zero correctly.
    • Single-digit numbers are also handled correctly by the loop and return comparison.

    Time and Space Complexity:

    Time Complexity: The time complexity is “O(log10​ . N)”, where “N” is the absolute value of the input number. This is because the number of iterations in the loop is proportional to the number of digits in the number.

    Space Complexity: The space complexity is “O(1)”. The function uses a fixed amount of space regardless of the input size.


    For any changes to the document, kindly email at code@codeanddebug.in or contact us at +91-9712928220.

    Math
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleLeetcode #7 : Reverse Integer Python Program Explained
    Next Article Python Program to Check Armstrong Number | Explained
    codeanddebug
    • Website

    Related Posts

    Data Structures & Algorithms

    Shortest Path in a Weighted Directed Acyclic Graph | Topological-Sort + Relaxation | Python

    17 June 2025
    Data Structures & Algorithms

    Shortest Path in an Undirected Graph with Unit Distance – Clear BFS Solution in Python

    13 June 2025
    Data Structures & Algorithms

    Check if array is sorted | Explained using Python Code

    13 June 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • Data Structures & Algorithms (50)
      • Beginner (24)
      • Expert (9)
      • Intermediate (17)
    • Uncategorised (1)
    Recent Posts

    Shortest Path in a Weighted Directed Acyclic Graph | Topological-Sort + Relaxation | Python

    17 June 2025

    Shortest Path in an Undirected Graph with Unit Distance – Clear BFS Solution in Python

    13 June 2025

    Check if array is sorted | Explained using Python Code

    13 June 2025

    Find the second largest and second smallest element in an Array | Explained

    13 June 2025

    Quick Sort Algorithm in Python | Explained

    13 June 2025
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.