Java String Reverse

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


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

Constraints

  •  will consist at most  lower case english letters.

Sample Input

madam

Sample Output

Yes
SOLUTIONS:
import java.util.Scanner;

/* If a String is equivalent to itself when reversed, it's a palindrome */
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 */
        String reversed = new StringBuilder(str).reverse().toString();
        System.out.println(str.equals(reversed) ? "Yes" : "No");
    }
}

Comments

Popular posts from this blog

Valid Username Regular Expression

Java SHA-256

Java Interface