A palindrome is a sequence of characters that reads the same backward as forward, such as "madam" or "121". Checking for palindromes is a common problem in programming tests and interviews. In Java, this can be approached using various methods including string manipulation and mathematical operations for numbers.
In this article, you will learn how to determine if a given input, either a string or a number, is a palindrome using Java. Explore practical examples that demonstrate both string manipulation and mathematical techniques to identify palindromic sequences efficiently.
Convert the string to lower case to make the check case-insensitive.
Reverse the string and compare it with the original string.
public static boolean isPalindromeString(String text) {
String clean = text.toLowerCase();
String reverse = new StringBuilder(clean).reverse().toString();
return clean.equals(reverse);
}
This code snippet defines a method isPalindromeString
that first normalizes the input string by converting it to lower case to ensure the check is case-insensitive. It then reverses the string using Java's StringBuilder
class and checks if the reversed string is equal to the original one.
Convert the string to lower case to ensure uniformity.
Use two pointers to compare characters from both ends moving towards the center.
public static boolean isPalindromeStringEfficient(String text) {
String clean = text.toLowerCase();
int left = 0;
int right = clean.length() - 1;
while (left < right) {
if (clean.charAt(left) != clean.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
This method utilizes two pointers, left
and right
, to start comparing characters from the start and end of the string. If any mismatch is found, it immediately returns false
. This method is more efficient than reversing the entire string as it can terminate early on a mismatch.
Convert the number to a string.
Use the string palindrome check to determine if the number is a palindrome.
public static boolean isPalindromeNumber(int number) {
return isPalindromeString(String.valueOf(number));
}
This approach utilizes the previously defined isPalindromeString
method by first converting the number into a string and then passing it to the method. It's a simple way that leverages reusability of code but involves converting numeric data to string.
Reverse the digits of the number mathematically.
Compare the reversed number with the original.
public static boolean isPalindromeNumberEfficient(int number) {
if (number < 0) return false; // Negative numbers cannot be palindrome
int reversed = 0, original = number;
while (number != 0) {
reversed = reversed * 10 + number % 10;
number /= 10;
}
return original == reversed;
}
This function handles the number directly without conversion to a string. It repeatedly peels off the last digit of the number and builds a new reversed number. Finally, it checks if the reversed number matches the original. This method is memory efficient as it doesn't create additional string objects.
Accurately checking for palindromes in both strings and numbers is a foundational skill in Java programming. By implementing the methods described, efficiently determine the palindromic nature of inputs in your Java applications. Utilize the string-based methods for quick implementations and the mathematical method for a more efficient approach when dealing with numbers. By mastering these techniques, ensure your programs can handle such checks in both algorithmic challenges and real-world applications effectively.