백준 1316번

김경욱·2025년 8월 2일

백준

목록 보기
16/121

// Press Shift twice to open the Search Everywhere dialog and type show whitespaces,
// then press Enter. You can now see whitespace characters in your code.
import java.util.Scanner;

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

    int time = in.nextInt();
    in.nextLine();
    String[] words = new String[time];
    // 우리가 구하는 중복이 없는 단어의 수 : count
    int count = 0;






    // time=3

    for (int i  = 0; i< time; i++)
    {
        words[i] = in.nextLine();

    }

    // words[0]-> happy   words[1] -> new   words[2] -> year

    for (int i =0;i < time; i++)
    {
        boolean[] alpha = new boolean[26];

        boolean isGroupWord = true;
        //prev는 단어의 한 알파벳 현재는 안 정해져서 0
        char prev = 0;
        for (int j=0; j < words[i].length(); j++)
        {
            char ch = words[i].charAt(j);
            if ( ch != prev) { //happy
                if(alpha[ch-'a']){
                    isGroupWord = false;
                    break;
                }
                alpha[ch-'a'] = true;

        }
            prev = ch;

        }
        if (isGroupWord){
            count++;
        }
    }


    System.out.println(count);





}

}

솔직히 말해서 내가 푼 문제는 아니다. 거의 다 지피티를 통해서 답을 도출했고 나는 로직을 이해하는 것만 해도 힘들었다. 확실히 실버5문제는 아직 나에게 너무 어려운 것 같다. boolean[] alpha = new boolean[26];으로 알파벳 배열을 만들고 alpha[ch-'a']를 이용하여 알파벳 배열을 구한다는 방식이 너무나도 신박했다. 또한 char prev를 선언함으로써 이전에 나온 알파벳과 지금 알파벳을 비교하는 것도 정말 좋은 방식이라고 생각하였다. 이 문제는 로직 이해하는것만으로도 나에게 버거운 문제인 것 같다. 아직 갈길이 먼 것 같다.

0개의 댓글