Java program to check palindrome (using library methods)
Last Updated :
13 Mar, 2023
Given a string, write a Java function to check if it is palindrome or not. A string is said to be palindrome if reverse of the string is same as string. For example, “abba” is palindrome, but “abbc” is not palindrome. The problem here is solved using string reverse function. Examples:
Input : malayalam
Output : Yes
Input : GeeksforGeeks
Output : No
Java
import java.io.*;
public class Palindrome
{
public static void checkPalindrome(String s)
{
String reverse = new StringBuffer(s).reverse().toString();
if (s.equals(reverse))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main (String[] args)
throws java.lang.Exception
{
checkPalindrome( "malayalam" );
checkPalindrome( "GeeksforGeeks" );
}
}
|
Time complexity: O(N), where N is length of given string.
Auxiliary Space: O(N), The extra space is used to store the reverse of the string.
Related Article : C program to check whether a given string is palindrome or not
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...