BackJoon Algorithm 10820 문자열 분석 (Java)

업데이트:
최대 1 분 소요

BackJoon Algorithm - Java

alt

문제

alt

풀이

  • 아스키 코드 숫자를 이용해 문제를 푼다.
  • 알파벳 배열을 만들고 (26칸) 소문자는 -97을 해주면 0부터 26까지 나온다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Back_10809 {
    public static void main(String[] args) throws IOException {

        // given
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int Alphabet[] = new int[26];
        for (int i = 0; i < Alphabet.length; i++) {
            Alphabet[i] = -1;                     // 먼저 알파벳 배열에 모두 -1 주입
        }
        // when
        for (int i = 0; i < str.length(); i++) {
            int Alphabet_count = str.charAt(i) - 97;
            if (Alphabet[Alphabet_count] != -1) {   // 먼저 들어간 알파벳이 있다면 건너뛰기
                continue;
            } else {
                Alphabet[Alphabet_count] = i;
            }
        }
        // then
        for (int i : Alphabet) {
            System.out.print(i+" ");
        }
        br.close();
    }
}





댓글남기기