본문 바로가기

Coding Test/백준

[백준/자바] 1316 - 그룹 단어 체커

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        int result = 0;
        for (int i = 0; i < N; i++) {
            char[] arr = sc.next().toCharArray();
            int[] index = new int[26];

            for (int j = 0; j < 26; j++) {
                index[j] = -1;
            }

            boolean isGroupWord = true;
            for (int k = 0; k < arr.length; k++) {
                if (index[arr[k] - 97] != -1 && k - index[arr[k] - 97] != 1) {
                    isGroupWord = false;
                    break;
                } else {
                    index[arr[k] - 97] = k;
                }
            }

            if (isGroupWord) result++;
        }

        System.out.println(result);
    }
}