티스토리 뷰

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

class Solution {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int T = Integer.parseInt(br.readLine());
        for (int t = 1; t <= T; t++) {
            int n = Integer.parseInt(br.readLine());
            String word = br.readLine();
            if (isMakeOk(word)) {
                sb.append(String.format("#%d %s\n", t, "Yes"));
            } else {
                sb.append(String.format("#%d %s\n", t, "No"));
            }
        }

        System.out.println(sb);
    }

    static boolean isMakeOk(String str) {
        if (str.length() % 2 != 0) {
            return false;
        }

        Stack<Character> stack = new Stack<>();
        int mid = str.length() / 2;
        for (int i = str.length() - 1; i >= mid; i--) {
            stack.push(str.charAt(i));
        }

        int idx = 0;
        while (!stack.isEmpty()) {
            char c = stack.pop();
            if (c != str.charAt(idx++)) {
                return false;
            }
        }

        return true;
    }
}
Total
Today
Yesterday
최근에 올라온 글
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30