Close Menu

    Maximum Consecutive Ones | Leetcode 485 | Explained with Images

    March 24, 2025

    Find missing number in an array | Leetcode 268 | Explained

    March 14, 2025

    Union of 2 Sorted Arrays with Duplicates | Explained with Images

    March 13, 2025

    Linear Search in Python | Explained with Images and Examples

    March 10, 2025
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram Pinterest Vimeo
    Code and Debug AcademyCode and Debug Academy
    Subscribe
    Code and Debug AcademyCode and Debug Academy
    Home»Training»Palindrome Number Program in Python | Leetcode #9
    Training

    Palindrome Number Program in Python | Leetcode #9

    Code and DebugBy Code and DebugDecember 30, 2024No Comments4 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Palindrome Number Program in Python
    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.

    Contents
     [show]
    • Examples of Palindrome Number Program in Python:
    • 1.Optimal Solution
      • Problem Statement:
      • Intuition and Approach:
      • Code:
      • Dry Run:
      • Potential Edge Cases:
      • Handling Edge Cases:
      • Time and Space Complexity:

    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.

    Constraints: -231 <= x <= 231 – 1

    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:

    Dry Run of Palindrome Program in Python

    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.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Code and Debug
    • Website

    Related Posts

    Training

    Best Data Analytics Training in Kochi for Clearing Placements

    February 26, 2025
    Training

    Best Data Analytics Course in Noida using Python

    February 25, 2025
    Training

    Best Data Analytics Training in Surat for Placement Interviews

    February 18, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Facebook X (Twitter) Instagram Pinterest
    © 2025 ThemeSphere. Designed by ThemeSphere.

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