Palindrome number: Leetcode solution

Palindrome number(leetcode)

Palindrome number in javascript


Determining if a given number is palindrome or not is a common problem asked in coding interviews and competitive programming. A palindrome number is a number that reads backward and forward  same, like 121 

Problem Statement

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

An integer is a palindrome when it reads the same backward as forward.

  • For example, 121 is a palindrome while 123 is not.

 

Examples:-

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

 

Follow up: Could you solve it without converting the integer to a string?

Approaches to Solve the Problem


My first approach is to convert the integer to a string then traverse the string in reverse and then match with it the given  [without inbuilt function] 

so let's convert it into a string by assigning a variable  "text=x" After that decrement the string and put it into a "bag" variable so now we have the reverse of the string and now compare it with it a string.

if it equals print  true else print false ;

 /**

* @param {number} x

 * @return {boolean}

 */

var isPalindrome = function(x) {

    let text="";

    text=text+x;

   let bag="";

        for(let i=text.length-1; i>=0;  i--)

        {

            bag=bag+text[i];

        }

    if(bag==text)

        {

            return true;

}

    else{

        return false;

    }

};


Accepted236 ms51.2 MBjavascript



2. The second approach , convert it into using an inbuilt function and then solve it 


 /**

* @param {number} x

 * @return {boolean}

 */

var isPalindrome = function(x) {

         let text=x.toString()

    let bag="";

        for(let i=text.length-1; i>=0;  i--)

        {

            bag=bag+text[i];

        }

    if(bag==text)

        {

            return true;

}

    else{

        return false;

    }

};

Accepted338 ms51.1 MBjavascript


3. Third approach  is not converting an integer to string here we solve it through modulus and division approach or palindrome number without converting to string



 @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {

// check the integer x if it is palindrome or not;
let temp=x;
     let rev=0;
    if(x<0){
        return false;
    }
    while(x!=0)
        {
        rev=rev*10+x%10;
            x=Math.floor(x/10);
        }if(temp==rev)
            {
                return true;
            }else {
                return false;

            }
};


Conclusion:-

Checking if a number is a palindrome can be approached in various ways, each with its own merits. The first two approaches involve converting the number to a string, which is simple and intuitive but may not be the most efficient. The third approach uses mathematical operations to avoid string conversion, making it more efficient for larger numbers.

By understanding and implementing these methods, you can effectively determine if an integer is a palindrome, preparing you well for coding interviews and algorithm challenges.

For more coding solutions and algorithm tips, make sure to follow our blog and stay updated with the latest programming practices



Post a Comment

Previous Post Next Post