코린이의 소소한 공부노트

[백준 온라인 저지] 10809. 알파벳 찾기 본문

코딩테스트 풀이/JAVA

[백준 온라인 저지] 10809. 알파벳 찾기

무지맘 2023. 3. 7. 16:16

- 입력: 첫째 줄에 단어 S가 주어진다. 단어의 길이는 100을 넘지 않으며, 알파벳 소문자로만 이루어져 있다.

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

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        char[] input = br.readLine().toCharArray();
        int[] count = new int[26];
        for(int i=0 ; i<26 ; i++)
            count[i] = -1;
        for(int i=0 ; i<input.length ; i++){
            if(count[input[i]-97]==-1)
                count[input[i]-97] = i;
        }
        for(int i=0; i<26; i++)
            bw.write(count[i]+" ");
        bw.flush();
        bw.close();
    }
}