Java Program to Check Whether a String is a Palindrome

Last Updated : 18 Feb, 2026

A palindrome string is a string that reads the same forward and backward. In simple words, if we reverse the string and it remains identical to the original, then it is a palindrome.

Example:

Input: s = "level"
Output: True

Input: s = "Geeks"
Output: True

Input s = "abc"
Output: False.

There are multiple approaches to check whether a string is a palindrome or not. Let's discuss one by one.

1. Brute Force Approach

The simplest way to check a palindrome is to reverse the string and compare it with the original string. To handle case-insensitive comparisons, we convert the string to lowercase.

Algorithm

  • Convert the string to lowercase
  • Reverse the string using a loop
  • Compare original string and reversed string
  • If both are equal -> palindrome, else not palindrome
Java
public class GFG{

    public static boolean isPalindrome(String s){

        // Convert to lowercase for case-insensitive check
        s = s.toLowerCase();

        // Reverse the string
        String rev = "";
        for (int i = s.length() - 1; i >= 0; i--) {
            rev = rev + s.charAt(i);
        }

        // Compare original and reversed
        return s.equals(rev);
    }

    public static void main(String[] args) {

        String s = "level";

        if (isPalindrome(s)) {
            System.out.println("\"" + s + "\" is a palindrome.");
        } else {
            System.out.println("\"" + s + "\" is not a palindrome.");
        }
    }
}

Output
"level" is a palindrome.

Explanation: In the above example, It checks if the given string is palindrome by converting it to lowercase, reversing it, and then comparing the reversed string with the original.

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2. Two Pointer Approach

This approach uses two pointers, one starting at the beginning "i" and the other at the end "j" of the string. By comparing characters at these pointers and moving them inward, we can determine if the string is a palindrome or not.

Algorithm

  • We initialize two pointers (i=0) from the start and (j = s.length -1) from the end.
  • Compare the characters at index i and j, if both are not equal then return false( it is not a palindrome).
  • If they match then increment i by 1 and decrement j by 1 and continue the process.
  • If all the characters match and the loop stops at ( i = j), then it is a palindrome and returns true.
Java
public class GFG{

    public static boolean isPalindrome(String s){

        s = s.toLowerCase();
        int i = 0, j = s.length() - 1;

        while (i < j) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }

    public static void main(String[] args){

        String s1 = "geeks";
        String s2 = "Racecar";

        System.out.println("\"" + s1 + "\" palindrome? " + isPalindrome(s1));
        System.out.println("\"" + s2 + "\" palindrome? " + isPalindrome(s2));
    }
}

Output
"geeks" palindrome? false
"Racecar" palindrome? true

Explanation: In this example, the isPalindrome method checks for mismatched characters while i < j and returns false if found. The main method converts input strings to lowercase and prints whether they are palindrome.

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3. Recursive Approach

Now for recursion we are using the same approach as we used in the two pointer, here we create two pointer I from start and j from the end in the recursion. and Increaement the i by one and decrement j by 1, if the characters at the index i not match the character present at index j then it is not a palidrome if it the base condition hit i=j and

Algorithm

  • Take two pointers: i = 0 (start) and j = n-1 (end)
  • Base Case: If i >= j, return true (palindrome confirmed)
  • If s.charAt(i) != s.charAt(j), return false
  • Otherwise, call recursion for next characters: isPalindrome(i+1, j-1)
  • If all pairs match, the string is a palindrome
Java
public class GFG{

    public static boolean isPalindrome(String s, int i, int j) {

        if (i >= j) return true;

        if (s.charAt(i) != s.charAt(j)) return false;

        return isPalindrome(s, i + 1, j - 1);
    }

    public static void main(String[] args) {

        String s = "Racecar";
        s = s.toLowerCase();

        System.out.println(isPalindrome(s, 0, s.length() - 1));
    }
}

Output
true
  • Time Complexity: O(n)
  • Space Complexity: O(n)
Comment