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
test
baab
uigui
No
Yes
Yes
Solution:
01 | import java.util.Scanner; |
02 | import java.util.Stack; |
03 |
04 | public 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 | } |
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