백준 - 좋은 단어

백준 - 좋은 단어

1. 유형

스택

2. 풀이

간단한 스택문제입니다.

3. 코드

import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine()); int N = stoi(st.nextToken()); int cnt= 0 ; while(N-->0) { st = new StringTokenizer(bf.readLine()); char[] c = st.nextToken().toCharArray(); Stack stack = new Stack<>(); for(char k: c) { if(!stack.isEmpty() && stack.peek() == k) { stack.pop(); }else { stack.add(k); } } if(stack.isEmpty()) { cnt++; } } System.out.println(cnt); } static int stoi(String s) { return Integer.valueOf(s); } }

from http://moons-memo.tistory.com/259 by ccl(A) rewrite - 2021-10-30 22:01:33