10809번: 알파벳 찾기

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

www.acmicpc.net

 

역시나 간단하게 해결할 수 있었다.

문제간단설명)

알파벳이 나오는 위치 출력

 

풀이)

알파벳이 단어에 포함되어 있지 않다면 -1출력해야한다.

우선 배열의 모든 값을 -1로 초기화.

그리고 알파벳을 받았을 때 인덱스 값을 각 알파벳 자리에 맞게 넣어준다.

indexof()를 사용한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.*;
public 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));
        int[] cnt = new int[26];
 
        String input = br.readLine();
 
        for(int i = 0; i < cnt.length; i++){
            cnt[i] = -1;
        }
        for(int i = 0; i < input.length(); i++){
            cnt[input.charAt(i) - 'a'= input.indexOf(input.charAt(i));
        }
 
        for(int i:cnt){
            bw.write(i + " ");
        }
        bw.flush();
        bw.close();
    }
}
 
 
cs

+ Recent posts