Check if a String is a Palindrome using Stacks in Java

Problem:

You are given a list of Strings as input. Check whether each string is a palindrome or not using a stack data structure. The first line of input n is the number of test cases followed by n strings.

Output:

3
test
baab
uigui

No
Yes
Yes

Solution:

01import java.util.Scanner;
02import java.util.Stack;
03 
04public class ProblemA {
05 
06 public static void main(String[] args) {
07   
08  Stack<Character> stack = new Stack<Character>();
09   
10  Scanner scan = new Scanner(System.in);
11   
12  int numberOfExpressions = scan.nextInt();
13  String[] expression = new String[numberOfExpressions];
14   
15  for (int i = 0;i<numberOfExpressions;i++)
16  {
17   expression[i] = scan.next();
18  }
19   
20  for (int i = 0;i<numberOfExpressions;i++)
21  {     
22   String checker = "";
23    
24   for (int j = 0; j < expression[i].length(); j++)
25    stack.push(expression[i].charAt(j));
26    
27   while (!stack.isEmpty())
28    checker += stack.pop();
29    
30   if (checker.equals(expression[i]))
31    System.out.println("Yes");
32   else
33    System.out.println("No");
34   
35  }
36 }
37}


1 comment :

  1. liposuction costs Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact