09. Palindrome Number LeetCode Solution in Java – Check if a Number is a Palindrome or Not

0
267

Introduction

Welcome to another exciting journey into the world of coding and problem-solving! In this blog, we will explore the concept of palindromic numbers and tackle a LeetCode problem that asks us to determine whether a given integer is a palindrome. We’ll dive into the problem, break it down, and provide a step-by-step solution in Java. Don’t worry if you’re new to coding; we’ll explain everything in simple language with plenty of examples.

What is a Palindrome Number?

A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). For instance, the word “racecar” is a palindrome because it reads the same in reverse. Similarly, numbers can also be palindromes. For example, 121 and 1331 are palindromic numbers.

The Problem Statement

LeetCode is a popular platform for practicing coding problems, and one of their problems is to determine whether a given integer is a palindrome. The problem statement is as follows:

Problem: Given an integer x, return true if x is a palindrome. An integer is a palindrome when it reads the same backward as forward. For example, 121 is a palindrome, but 123 is not.

Now, let’s break down the problem and come up with a step-by-step solution.

Step 1: Convert the Integer to a String

The first thing we need to do is convert the integer into a string. This is because it’s much easier to check for palindromes in a string than in an integer. In Java, you can convert an integer to a string using the Integer.toString() method. Here’s how you can do it:

int num = 121;
String str = Integer.toString(num);

Now, the variable str contains the string representation of the integer num, which is “121” in this case.

Step 2: Check for Palindrome

To check if a string is a palindrome, we need to compare it with its reverse. If they are the same, the string (and thus the integer) is a palindrome; otherwise, it’s not. Here’s how you can check for a palindrome in a string:

boolean isPalindrome = str.equals(new StringBuilder(str).reverse().toString());

Let’s break down this line of code:

  • new StringBuilder(str): This creates a StringBuilder object initialized with the string str.
  • .reverse(): This reverses the characters in the StringBuilder.
  • .toString(): This converts the reversed StringBuilder back to a string.
  • .equals(str): This compares the reversed string with the original string to check if they are the same.

The variable isPalindrome will be true if the string is a palindrome and false if it’s not.

Step 3: Putting It All Together

Now that we have the two essential steps in place, let’s put them together into a Java function that checks if an integer is a palindrome:

public class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false; // Negative numbers are not palindromes.
        }

        String str = Integer.toString(x);
        return str.equals(new StringBuilder(str).reverse().toString());
    }
}

This Java function takes an integer x as input and returns true if it’s a palindrome and false otherwise. It first checks if the input is negative because negative numbers cannot be palindromes by definition.

Examples

Let’s test our isPalindrome function with some examples to see how it works:

public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();

        // Example 1: Palindrome number
        int num1 = 121;
        System.out.println("Is " + num1 + " a palindrome? " + solution.isPalindrome(num1)); // Output: true

        // Example 2: Not a palindrome number
        int num2 = -121;
        System.out.println("Is " + num2 + " a palindrome? " + solution.isPalindrome(num2)); // Output: false

        // Example 3: Not a palindrome number
        int num3 = 10;
        System.out.println("Is " + num3 + " a palindrome? " + solution.isPalindrome(num3)); // Output: false

        // Example 4: Palindrome number with odd digits
        int num4 = 12321;
        System.out.println("Is " + num4 + " a palindrome? " + solution.isPalindrome(num4)); // Output: true
    }
}

As you can see, our isPalindrome function correctly identifies whether the given integers are palindromes or not.

Complexity Analysis

Before we conclude, let’s briefly discuss the time and space complexity of our solution.

  • Time Complexity: The time complexity of our solution is O(n), where n is the number of digits in the integer. This is because we convert the integer to a string, which takes O(n) time, and then compare the string with its reverse, which also takes O(n) time.
  • Space Complexity: The space complexity is O(n) as well. This is because we create a string of length n to store the string representation of the integer, and we also create a StringBuilder to store the reversed string.

Conclusion

In this blog, we explored the concept of palindromic numbers and solved the Palindrome Number problem on LeetCode in Java. We learned how to convert an integer to a string and then check if it’s a palindrome by comparing it with its reverse.

Coding problems like these are not only fun but also help improve your problem-solving skills. Palindromes, in particular, are fascinating because they occur in various contexts, from words to numbers.

If you’re new to programming, don’t be discouraged if you didn’t fully grasp all the details in one go. Learning to code is a journey, and practice is key. Try out different examples and modify the code to see how it behaves. As you continue to solve problems and build projects, you’ll become a more proficient programmer.

Keep exploring, keep coding, and remember that every line of code you write is a step toward becoming a better programmer. Happy coding!

Thank You!

Read More –

590. N-ary Tree Post order Traversal | n Array Tree Post Order Traversal LeetCode Solution in Java – https://kamleshsingad.com/590-n-ary-tree-post-order-traversal-n-array-tree-post-order-traversal/

94. Binary Tree Inorder Traversal | Binary Tree Inorder Traversal LeetCode Solution in Java – https://kamleshsingad.com/94-binary-tree-inorder-traversal-binary-tree-inorder-traversal-leetcode-solution-in-java/

589. N-ary Tree Preorder Traversal | N-ary Tree Preorder Traversal LeetCode Solution in Java – https://kamleshsingad.com/589-n-ary-tree-preorder-traversal-n-ary-tree-preorder-traversal-leetcode-solution-in-java/

LEAVE A REPLY

Please enter your comment!
Please enter your name here