Efficient Way to Check String is Palindrome or Not

A String can be Palindrome if that reads the same forward or reversed. For example, ‘radar’, ‘level’ , ‘refer’ are some Palindrome words.
A String is palindrome or not can be checked using different approaches but the best approach is to compare string first and last character and then proceed further
Program to check Palindrome using above approach

import java.util.Scanner;

public class Palindrome {	
	
	public static void main(String[] args){
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Please enter the String");
		
		String str = sc.nextLine();
		
		sc.close();
		
		char[] word = str.toCharArray();
		
		boolean status = isPalindrome(word);
		
		if(status == true){
			System.out.println("Given String is Palindrome");
		}
		else{
			System.out.println("String is not Palindrome");
		}
		
	}

	public static boolean isPalindrome(char[] word){
	    int start = 0;
	    int end = word.length - 1;
	    while (end > start) {
	        if (word[start] != word[end]) {
	            return false;
	        }
	        ++start;
	        --end;
	    }
	    return true;
	}
}

other approach can be to reverse the string and the compare the original with revers string, if both string are equal it means they are Palindrome, otherwise they are not
Code using above approach

import java.util.Scanner;
public class Palindrome {
		
	public static void main(String[] args){
		
	    String original, reverse = "";

	    Scanner sc = new Scanner(System.in);
		
	    System.out.println("Please enter the String");
		
	    original = sc.nextLine();
		
	    sc.close();
			 
	    int length = original.length();
		 
		for ( int i = length - 1; i >= 0; i-- ){
		     reverse = reverse + original.charAt(i);
		}
		 
		      if (original.equals(reverse))
		         System.out.println("Entered string is a palindrome.");
		      else
		         System.out.println("Entered string is not a palindrome.");
		 
		  }
	}	

String is Palindrome or not can also be checked using recursion
Program using recursion

import java.util.Scanner;


public class Palindrome {
	
	
	public static void main(String[] args){
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Please enter the String");
		
		String str = sc.nextLine();
		
		sc.close();
		
		boolean status = isPalindrome(str);
		
		if(status == true){
			System.out.println("Given String is Palindrome");
		}
		else{
		    System.out.println("String is not Palindrome");
		}
		
		
	}

	private static boolean isPalindrome(String word) {
		 if(word.length() < 2) { 
				  return true;  
			}
		char first  = word.charAt(0);
		char last   = word.charAt(word.length()-1);
	    if(  first != last  ) { 
	    	return false; 
	    }
		else { 
			return isPalindrome(word.substring(1,word.length()-1)); 
		}
	}
}	

A String is Palindrome or not can be checked using the different logic’s, but the best one is to compare the starting character and last character and proceed further.

Leave a Comment