Java Program to Check Palindrome String
boolean isPalindrome = true; for (int i = 0; i < str.length()/2; i++) { if (str.charAt(i) != str.charAt(str.length() - 1 - i)) { isPalindrome = false; break; } }.Examples
How to Think About It
Algorithm
Code
public class PalindromeCheck { public static void main(String[] args) { String str = "madam"; boolean isPalindrome = true; for (int i = 0; i < str.length() / 2; i++) { if (str.charAt(i) != str.charAt(str.length() - 1 - i)) { isPalindrome = false; break; } } if (isPalindrome) { System.out.println(str + " is a palindrome"); } else { System.out.println(str + " is not a palindrome"); } } }
Dry Run
Let's trace the string "madam" through the code to see how it checks for palindrome.
Initialize variables
str = "madam", isPalindrome = true
First iteration (i=0)
Compare str.charAt(0) = 'm' and str.charAt(4) = 'm' -> equal
Second iteration (i=1)
Compare str.charAt(1) = 'a' and str.charAt(3) = 'a' -> equal
Loop ends
All compared characters matched, isPalindrome remains true
Print result
Output: "madam is a palindrome"
| i | str.charAt(i) | str.charAt(str.length()-1-i) | Match? |
|---|---|---|---|
| 0 | m | m | Yes |
| 1 | a | a | Yes |
Why This Works
Step 1: Compare characters from ends
The code compares characters starting from the first and last positions moving inward using charAt.
Step 2: Stop if mismatch found
If any pair of characters differ, the loop breaks early and sets isPalindrome to false.
Step 3: Confirm palindrome
If all pairs match, the string is confirmed as a palindrome and the program prints the positive result.
Alternative Approaches
public class PalindromeCheck { public static void main(String[] args) { String str = "madam"; String reversed = new StringBuilder(str).reverse().toString(); if (str.equals(reversed)) { System.out.println(str + " is a palindrome"); } else { System.out.println(str + " is not a palindrome"); } } }
public class PalindromeCheck { public static boolean isPalindrome(String str, int start, int end) { if (start >= end) return true; if (str.charAt(start) != str.charAt(end)) return false; return isPalindrome(str, start + 1, end - 1); } public static void main(String[] args) { String str = "madam"; if (isPalindrome(str, 0, str.length() - 1)) { System.out.println(str + " is a palindrome"); } else { System.out.println(str + " is not a palindrome"); } } }
Complexity: O(n) time, O(1) space
Time Complexity
The program checks up to half the string length, so it runs in linear time O(n), where n is the string length.
Space Complexity
It uses only a few variables and no extra data structures, so space complexity is O(1).
Which Approach is Fastest?
The loop comparison method is fastest and uses least memory compared to reversing the string or recursion.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Loop character comparison | O(n) | O(1) | Memory efficient and fast |
| StringBuilder reverse | O(n) | O(n) | Simple code but uses extra memory |
| Recursive check | O(n) | O(n) | Elegant but uses stack space |