Home >Java >javaTutorial >Strings: Checking for Palindromes
In this post, we’ll walk through a common interview question—checking if a given string is a palindrome. This problem is a great exercise for understanding pointers, loops, and conditional logic in Java.
Write a Java method that checks if a given string is a palindrome. A palindrome is a word or phrase that reads the same forwards and backwards (e.g., “noon” or “madam”).
The solution leverages a two-pointer technique to check characters from both ends of the string, moving towards the center. By comparing characters at corresponding positions, we can determine if the string is a palindrome without having to reverse it.
Here’s the code for the solution:
public class StringPalindromeQuestion { // Method to check if a given string is a palindrome private boolean isPalindrome(String string) { if (string != null) { for (int i = 0, j = string.length() - 1; i < string.length() / 2; i++, j--) { if (string.charAt(i) != string.charAt(j)) { return false; } } } return true; } public static void main(String[] args) { StringPalindromeQuestion palindrome = new StringPalindromeQuestion(); String oddString = "abcdcba"; // Palindrome with odd length String evenString = "abccba"; // Palindrome with even length String nonPalindrome = "asfgsa"; // Not a palindrome // Result: true System.out.println(palindrome.isPalindrome(oddString)); // Result: true System.out.println(palindrome.isPalindrome(evenString)); // Result: false System.out.println(palindrome.isPalindrome(nonPalindrome)); // Testing with null // Result: true System.out.println(palindrome.isPalindrome(null)); } }
1. Two-Pointer Approach:
We initialize two pointers: one at the start (i) and one at the end (j).
We compare characters at these positions (string.charAt(i) and string.charAt(j)) and increment i and decrement j after each comparison.
The loop runs only up to string.length() / 2, ensuring efficient traversal regardless of whether the length is odd or even.
2. Odd vs. Even Length:
For even-length strings (e.g., "abccba"), the method checks up to the midpoint, so no middle character remains unchecked.
For odd-length strings (e.g., "abcdcba"), the middle character naturally does not affect palindrome status.
3. Null Handling:
The method checks if the string is null at the beginning to avoid NullPointerException.
Odd-length Palindrome: "abcdcba" returns true.
Even-length Palindrome: "abccba" returns true.
Non-Palindrome: "asfgsa" returns false.
Null String: returns true (a null input is considered a palindrome by this implementation).
Understanding two-pointer techniques is valuable for solving many string-based problems efficiently. This technique avoids extra space complexity and makes code execution faster by limiting unnecessary comparisons.
This solution provides a clean and efficient way to check for palindromes in Java. Try using this approach with different string inputs to further solidify your understanding of pointer manipulation and string traversal.
Happy Coding!
The above is the detailed content of Strings: Checking for Palindromes. For more information, please follow other related articles on the PHP Chinese website!