Java 8 coding challenge: Palindromic String

Problem:

You have been given a String SS. You need to find and print whether this string is a palindrome or not. If yes, print “YES” (without quotes), else print “NO” (without quotes).

Input Format
The first and only line of input contains the String SS. The String shall consist of lowercase English alphabets only.

Output Format
Print the required answer on a single line.

Constraints 1|S|1001≤|S|≤100

Note
String SS consists of lowercase English Alphabets only.

SAMPLE INPUT
aba
SAMPLE OUTPUT
YES

Code:

/* IMPORTANT: Multiple classes and nested static classes are supported */
 
/*
 * uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
*/
//import for Scanner and other utility classes
import java.util.*;
 
 
class TestClass {
 public static void main(String args[] ) throws Exception {
 /*
 * Read input from stdin and provide input before running
 * Use either of these methods for input
 
 //BufferedReader
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String line = br.readLine();
 int N = Integer.parseInt(line);
 */
 //Scanner
 Scanner s = new Scanner(System.in);
 String S = s.nextLine();
 String result="";
 
 for (int i = S.length()-1; i >=0; i--) {
 result=result+S.substring(i,i+1);
 }
 if(result.equals(S)){
 System.out.println("YES");
 }
 else{
 System.out.println("NO");
 }
 }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s