Check Palindrome
Given a non-negative integer num, check whether the given number is palindrome or not.
What is a palindrome?
A palindrome is a word, phrase, number, or any other sequence of characters that reads the same forward and backward.
Examples
Input: 121
Output: true
Explanation: The number 121 reads the same forward and backward.
Input: 123
Output: false
Explanation: The number 123 does not read the same backward (321), so it is not a palindrome.
Since we have learned about the concept of extracting the digits from a number and reversing a number let us now understand how those concepts will help with checking whether the given number is palindrome or not.
How to check whether a given number is palindrome or not?
To check whether a given number num is a palindrome, reverse the digits of the number num and store it in another variable. Check if the original number is equal to the reversed number. If the original and reversed numbers are the same, then the number is a palindrome otherwise, it is not.
Code Implementation
C++ Code Try on Compiler!
bool isPalindrome(int num) {
// Negative numbers cannot be palindromes
if (num < 0) return false;
int originalNum = num;
int reversedNumber = 0;
while (num > 0) {
int digit = num % 10;
reversedNumber = reversedNumber * 10 + digit;
num = num / 10;
}
return originalNum == reversedNumber;
}
Java Code Try on Compiler!
public class PalindromeCheck {
public static boolean isPalindrome(int num) {
if (num < 0) return false; // Negative numbers are not palindromes
int originalNum = num;
int reversedNumber = 0;
while (num > 0) {
int digit = num % 10;
reversedNumber = reversedNumber * 10 + digit;
num /= 10;
}
return originalNum == reversedNumber;
}
}
Python Code Try on Compiler!
def is_palindrome(num):
if num < 0:
return False # Negative numbers are not palindromes
original_num = num
reversed_number = 0
while num > 0:
digit = num % 10
reversed_number = reversed_number * 10 + digit
num = num // 10
return original_num == reversed_number
Javascript Code Try on Compiler!
function isPalindrome(num) {
if (num < 0) return false; // Negative numbers are not palindromes
let originalNum = num;
let reversedNumber = 0;
while (num > 0) {
let digit = num % 10;
reversedNumber = reversedNumber * 10 + digit;
num = Math.floor(num / 10);
}
return originalNum === reversedNumber;
}
Time Complexity : O(logn)
The time complexity arises from the number of digits in the number. The number of times the while loop runs depends on the number of digits in the given number num.
For a given number num in a decimal representation, the number of digits is approximately equal to log10(num). This is because each digit represents a power of 10.
The loop executes once for each digit in num.
Therefore, the loop runs log10(num) times, which simplifies to O(logn).
Space Complexity : O(1)
We are using only variables like reversedNumber, and originalnumber which use constant space.
Since the space required by these variables does not depend on the size of the input number num, the auxiliary space is O(1).
Now since we have learned about how to extract digits from a number, let's solve another problem that uses the concept of digit extracting.