자바/백준

[백준 10809] 알파벳 찾기

슈슈버거 2022. 9. 6. 19:50

문제

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

 

10809번: 알파벳 찾기

각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출

www.acmicpc.net


내가 작성한 코드

import java.io.*;
import java.util.*;

public class 백준10809 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String alphabet = "abcdefghijklmnopqrstuvwxyz";
        int[] state = new int[26];

        for(int i = 0; i<26; i++){
            state[i] = -1;
        }

        String input = br.readLine();

        for(int i = 0; i<input.length(); i++){
            int a = alphabet.indexOf(input.charAt(i));
            if(state[a] == -1){
                state[a] = i;
            }
        }

        for(int i = 0; i<26; i++){
            bw.write(Integer.toString(state[i]) + " ");
        }
        bw.close();
    }
}