Java Palindrome Program

In this tutorial, we will learn how to write a Java program to check if a given string is a palindrome or not using different methods and techniques. We will cover below methods to check if a string is a palindrome or not:
  • Using For Loop
  • Using StringBuilder
  • Using Stream API

What is Palindrome?

A palindrome is a string or number that if you read it from the end to the beginning, it will be the same as reading from the beginning to the end. For example:

  • "madam" is a palindrome.
  • "racecar" is a palindrome.
  • "level" is a palindrome.
If you read any of these words from the end to the beginning, it will be the same as reading from the beginning to the end.

Java Program to Check if a String is Palindrome using For Loop

In this program, we will check if a given string is a palindrome or not using a for loop. We will compare the characters from the start and end of the string and check if they are equal. We will follow below steps to check if a string is a palindrome or not:

  1. Convert the given string to lowercase to ignore the case sensitivity.
  2. Remove all the special characters and spaces from the string.
  3. Initialize two pointers, one at the start of the string and the other at the end of the string.
  4. Compare the characters at the start and end of the string.
  5. If the characters are not equal, then the string is not a palindrome.
  6. If all the characters are equal, then the string is a palindrome.
public class PalindromeProgram {
    public static void main(String[] args) {

        String str = "madam";
        boolean isPalindrome = true;

        // Convert the string to lowercase
        str = str.toLowerCase();
        
        // Remove all special characters and spaces
        str = str.replaceAll("[^a-zA-Z0-9]", "");

        // Initialize two pointers
        int start = 0;
        int end = str.length() - 1;

        // Compare the characters at the start and end of the string
        while (start < end) {
            if (str.charAt(start) != str.charAt(end)) {
                isPalindrome = false;
                break;
            }
            start++;
            end--;
        }

        if (isPalindrome) {
            System.out.println("The given string is a palindrome.");
        } else {
            System.out.println("The given string is not a palindrome.");
        }
    }
}
Output: The given string is a palindrome.
Time Complexity: O(n) - Where n is the length of the string, as we are iterating through the string once.
Space Complexity: O(1) - Only uses a constant amount of extra space

Java Program to Check if a String is Palindrome using StringBuilder

In this program, we will check if a given string is a palindrome or not using StringBuilder. We will reverse the given string using StringBuilder and compare it with the original string. If both the strings are equal, then the given string is a palindrome. We will follow below steps to check if a string is a palindrome or not using StringBuilder:

  1. Convert the given string to lowercase to ignore the case sensitivity.
  2. Remove all the special characters and spaces from the string.
  3. Reverse the given string using StringBuilder.
  4. Compare the reversed string with the original string.
  5. If both the strings are equal, then the given string is a palindrome.
public class PalindromeProgram {
    public static void main(String[] args) {

        String str = "madam";
        boolean isPalindrome = isPalindrome(str);

        if (isPalindrome) {
            System.out.println("The given string is a palindrome.");
        } else {
            System.out.println("The given string is not a palindrome.");
        }

    }

    public static boolean isPalindrome(String str) {
        // Convert the string to lowercase
        str = str.toLowerCase();

        // Remove all special characters and spaces
        str = str.replaceAll("[^a-zA-Z0-9]", "");

        // Reverse the given string using StringBuilder
        StringBuilder reversedStr = new StringBuilder(str).reverse();
        
        // Compare the reversed string with the original string
        return str.equals(reversedStr.toString());
    }
}
Output: The given string is a palindrome.
Time Complexity: O(n) - Where n is the length of the string, as we are iterating through the string once.
Space Complexity: O(n) - Where n is the length of the string, as we are creating a new StringBuilder object to reverse the string.

Java Program to Check if a String is Palindrome using Java Stream API

In this program, we will check if a given string is a palindrome or not using Java Stream API. We will reverse the given string using Java Stream API and compare it with the original string. If both the strings are equal, then the given string is a palindrome. We will follow below steps to check if a string is a palindrome or not using Java Stream API:

  1. Convert the given string to lowercase to ignore the case sensitivity.
  2. Remove all the special characters and spaces from the string.
  3. Reverse the given string using Java Stream API.
  4. Compare the reversed string with the original string.
  5. If both the strings are equal, then the given string is a palindrome.
import java.util.stream.Collectors;

public class PalindromeProgram {
    public static void main(String[] args) {

        String str = "madam";
        boolean isPalindrome = isPalindrome(str);

        if (isPalindrome) {
            System.out.println("The given string is a palindrome.");
        } else {
            System.out.println("The given string is not a palindrome.");
        }

    }

    public static boolean isPalindrome(String str) {
        // Convert the string to lowercase
        str = str.toLowerCase();

        // Remove all special characters and spaces
        str = str.replaceAll("[^a-zA-Z0-9]", "");

        // Reverse the given string using Java Stream API
        String reversedStr = str.chars()
                .mapToObj(c -> String.valueOf((char) c))
                .collect(Collectors.joining());

        // Compare the reversed string with the original string
        return str.equals(reversedStr);
    }
}
Output: The given string is a palindrome. //new line
Time Complexity: O(n) - Where n is the length of the string, as we are iterating through the string once.
Space Complexity: O(n) - Where n is the length of the string, as we are creating a new string using Java Stream API.