Deque Both Ends

Lab Java Intermediate 20 points

Read a word, put its characters in an ArrayDeque, and repeatedly compare pollFirst with pollLast to decide if it's a palindrome. Print true/false.

Input

n, then n words.

Output

n lines.

Example

Input

3
racecar
java
noon

Output

true
false
true
Sample input available Sample output available
Editor
Java

Hints

A Deque supports adding and removing at both ends.

Solution explanation

Compare boxed Character objects with equals, not ==.

Show solution
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            Deque<Character> d = new ArrayDeque<Character>();
            for (char c : sc.next().toCharArray()) {
                d.addLast(c);
            }
            boolean pal = true;
            while (d.size() > 1) {
                if (!d.pollFirst().equals(d.pollLast())) {
                    pal = false;
                    break;
                }
            }
            System.out.println(pal);
        }
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD