Hackerrank Java String Reverse Solution
1 min read

Hackerrank Java String Reverse Solution

Hackerrank Java String Reverse Solution
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.(Wikipedia)

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

Solution in java8

Approach 1.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        
        
        if(A.equalsIgnoreCase(new StringBuffer(A).reverse().toString()))
            System.out.println("Yes");
        else
            System.out.println("No");
        
    }
}

Approach 2.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        /* Enter your code here. Print output to STDOUT. */
        String rev = "";
        for(int i=A.length()-1;i>=0;i--)
            rev+=A.charAt(i);
        if(rev.equals(A))
            System.out.println("Yes");
        else
                    System.out.println("No");

    }
}




Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×