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

다인·2024년 7월 8일

백준

목록 보기
6/112
post-thumbnail

해당 알파벳이 나온 적이 있는지 검사하는 배열은 int로 하느냐, boolean으로 하느냐에 차이를 두어보았다.
연속된 배열인지 확인하는 거 첨에 놓쳤고, 배열을 for문 안에 두어서 항상 초기화하기 놓치지 말자!

int 배열

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int count = N;

        for(int i=0; i<N; i++){
            String str = br.readLine();
            int[] arr = new int[26];

            for(int j=0; j<str.length(); j++){
                if(j>0 && str.charAt(j-1) == str.charAt(j))
                    continue;
                if(arr[str.charAt(j)-97] == 1){
                    count--;
                    break;
                }
                else
                    arr[str.charAt(j)-97] = 1;
            }
        }
        System.out.println(count);
    }
}

boolean 배열

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int count = N;

        for(int i=0; i<N; i++){
            String str = br.readLine();
            boolean[] arr = new boolean[26];

            for(int j=0; j<str.length(); j++){
                if(j>0 && str.charAt(j-1) == str.charAt(j))
                    continue;
                if(arr[str.charAt(j)-97]){
                    count--;
                    break;
                }
                else
                    arr[str.charAt(j)-97] = true;
            }
        }
        System.out.println(count);
    }
}

아주 근소하게 코드가 간결해진 거지 역시나 거의 차이 없당ㅎ

0개의 댓글