본문 바로가기
알고리즘/백준

백준 1316 그룹 단어 체커 자바

by reumiii 2019. 10. 24.
import java.util.*;
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++) {
			String word = sc.next();
			boolean check[] = new boolean[26];
			char pastCh = ' ';

			for (int j = 0; j < word.length(); j++) {
				if (pastCh != word.charAt(j)) {// 다른 알파벳이 나왔을때
					if (check[word.charAt(j) - 'a'] == true) {// 전에 나왔던 알파벳이면 그룹단어 아님.
						break;
					}
					check[word.charAt(j) - 'a'] = true;// 아직 나오지 않았던 알파벳이면 true 체크해주고 다음으로 넘김
				}
				
				pastCh = word.charAt(j);
				if(j==word.length()-1) {//마지막 알파벳까지 검토하여 ok이면 그룹단어의 개수 1 더함
					result++;
				}
			}
		}

		System.out.println(result);
    }
}

'알고리즘 > 백준' 카테고리의 다른 글

백준 1193 분수 찾기 자바  (0) 2019.10.31
백준 2292 벌집 자바  (0) 2019.10.30
백준 2839 설탕 배달 자바  (0) 2019.10.29
백준 1712 손익분기점 자바  (0) 2019.10.28
백준 2941 크로아티아 알파벳 자바  (0) 2019.10.23

댓글