LeetCode Problem #9: Palindrome Number - Explanation and Solutions in Python and JavaScript

LeetCode Problem #9: Palindrome Number - Explanation and Solutions in Python and JavaScript

LeetCode Problem #9: Palindrome Number - Explanation and Solutions in Python and JavaScript

Introduction

The Palindrome Number problem is a classic problem that tests your understanding of number manipulation and basic algorithms. It is frequently asked in coding interviews to assess your problem-solving skills. In this post, we'll break down the problem, explain the optimal approach, and provide solutions in both Python and JavaScript.

Problem Statement

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

Example:

Input: x = 121
Output: true
Explanation: 121 reads the same from left to right and right to left.
    

Approach

A palindrome number reads the same backward as forward. Here's the step-by-step approach to solve the problem:

  1. Handle edge cases: Negative numbers cannot be palindromes because of the negative sign.
  2. Reverse the second half of the number and compare it with the first half.
  3. If both halves are the same, the number is a palindrome.

Solution in Python


def isPalindrome(x):
    if x < 0 or (x % 10 == 0 and x != 0):
        return False

    reversed_num = 0
    original_num = x

    while x > 0:
        reversed_num = reversed_num * 10 + x % 10
        x = x // 10

    return original_num == reversed_num
        

Explanation: We first handle edge cases (negative numbers and numbers ending with 0). Then, we reverse the number and compare it with the original number. If they are the same, the number is a palindrome.

Solution in JavaScript


function isPalindrome(x) {
    if (x < 0 || (x % 10 === 0 && x !== 0)) {
        return false;
    }

    let reversedNum = 0;
    let originalNum = x;

    while (x > 0) {
        reversedNum = reversedNum * 10 + x % 10;
        x = Math.floor(x / 10);
    }

    return originalNum === reversedNum;
}
        

Explanation: Similar to the Python solution, we handle edge cases and reverse the number. We then compare the reversed number with the original number to determine if it's a palindrome.

Time and Space Complexity

Time Complexity: O(log(n)) - We reverse half of the number.
Space Complexity: O(1) - We use a constant amount of extra space.

Edge Cases

  • Negative numbers (e.g., -121).
  • Numbers ending with 0 (e.g., 10, 100).
  • Single-digit numbers (e.g., 0, 1, 9).

Conclusion

The Palindrome Number problem is a great way to practice number manipulation and edge case handling. By understanding this approach, you can solve similar problems like checking if a string is a palindrome or finding the largest palindrome in a given range. Try solving it on your own and explore related problems on LeetCode!

Comments

Popular posts from this blog

LeetCode Problem #10: Regular Expression Matching - Explanation and Solutions in Python and JavaScript

LeetCode Problem #1: Two Sum - Explanation and Solutions in Python and JavaScript

LeetCode Problem #2: Add Two Numbers - Explanation and Solutions in Python and JavaScript