Java String Reverse – Hacker Rank Solution

Java String Reverse - Hacker Rank Solution
Java String Reverse – Hacker Rank Solution

Problem :

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.


Given a string A , print Yes if it is a palindrome, print No otherwise.

Constraints

  • A will consist at most 50 lower case english letters.

Sample Input

madam

Sample Output

Yes


Java String Reverse – Hacker Rank Solution

import java.util.Scanner;
public class Solution {
    public static void main(String[] args) {
        /* Read input */
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        scan.close();
        /* Reverse string and compare to original */
        /* If a String is equivalent to itself when reversed, it's a palindrome */
        String reversed = new StringBuilder(str).reverse().toString();
        System.out.println(str.equals(reversed) ? "Yes" : "No");
    }
}

Disclaimer: The above Problem (Java String Reverse) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Leave a Comment