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.
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:
- Handle negative numbers separately, as they cannot be palindromes
- 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)
- Initialize a variable to store the reversed number
- Use a loop to extract the last digit of the number and build the reversed number
- 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
- Negative Number Check: The function immediately returns False if “x” is negative, as negative numbers cannot be palindromes.
- Initialize Variables: The variable “num” is initialized to “x” and “palindrome_number” is initialized to 0 to build the reversed number.
- Loop to Reverse Digits:
- While “num” is greater than 0, the last digit is extracted using num % 10.
- This digit is appended to “palindrome_number” by multiplying palindrome_number by 10 and adding the digit.
- The last digit is removed from “num” using integer division by 10.
- 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:
- Negative Numbers:
- Negative numbers should immediately return False.
- Single-Digit Numbers:
- Any single-digit number should return True as it is inherently a palindrome (e.g., x = 5).
- 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.