백준 1316 그룹 단어 체커

https://www.acmicpc.net/problem/1316

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int ans = 0;
        int n = sc.nextInt();
        String[] text = new String[n];
        for(int i = 0; i < n; i ++) {
            text[i] = sc.next();
        }

        for(int i = 0; i < n; i ++) {
            if(check(text[i]))
               ans++;
        }

        System.out.println(ans);
    }

    public static boolean check(String string) {
        HashSet<Character> set = new HashSet<>();
        set.add(string.charAt(0));
        for(int i = 1; i < string.length(); i ++) {
            char thisString = string.charAt(i);
            if(thisString == string.charAt(i-1))
                continue;
            if(!set.contains(thisString))
                set.add(thisString);
            else
                return false;
        }
        return true;
    }
}

좋은 웹페이지 즐겨찾기