백준 1316번 그룹 단어 체커(java)

마뇽미뇽·2024년 5월 6일
0

알고리즘 문제풀이

목록 보기
61/165

1.문제

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

2.풀이

전혀 감이 오지 않았던 문제..
https://st-lab.tistory.com/69 님과
https://dawning-record.tistory.com/32 님을 참고하였다.

3.코드

import java.util.Scanner;

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

        int n = sc.nextInt();
        int cnt = n;

        for(int i = 0; i < n; i++){
            int alpha[] = new int[26];
            String s = sc.next();
            int prev = 0;

            for(int j = 0; j < s.length(); j++){
                if(prev != s.charAt(j)){
                    if(alpha[s.charAt(j) - 'a'] == 0){
                        alpha[s.charAt(j) - 'a']++;
                        prev = s.charAt(j);
                    }
                    else {
                        cnt--;
                        break;
                    }
                }
            }
        }
        System.out.println(cnt);
    }
}
profile
Que sera, sera

0개의 댓글