백준 - 그룹 단어 체커(1316)

정민주·2024년 1월 27일

코테

목록 보기
6/95

⭐ 문제 : https://www.acmicpc.net/problem/1316

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int count = 0;
        int n = Integer.parseInt(br.readLine());
        
        for(int i=0; i<n; i++){
            if(check_word(br.readLine())){
                count++;
            }
        }
        System.out.println(count);
    }

    public static boolean check_word(String word){
        HashSet<Character> word_set = new HashSet<Character>();
        word_set.add((word.charAt(0)));

        for(int i=1; i<word.length(); i++){
            char exChar = word.charAt(i-1);
            char currentChar = word.charAt(i);

            if (word_set.contains(currentChar) && (exChar != currentChar)) {
                return false;
            }
            word_set.add(currentChar);
        }
        return true;
    }

}

0개의 댓글