본문 바로가기
코딩테스트/백준

Java - 백준 코딩테스트 10809번 [문자열 - 알파벳 찾기]

by sycareer 2021. 6. 25.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class FindAlphabet {
 
    public static void main(String[] args) throws IOException {
        // 20210625 - 10809번
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        String str = br.readLine();
 
        char word[] = new char[str.length()];
        
        for (int i = 0; i < word.length; i++) {
            word[i] = str.charAt(i);
        }
        
        int alpha = 97;
        int arr[] = new int[26];
        int result[] = new int[26];
        
        for (int i = 0; i < arr.length; i++) {
            arr[i] = alpha++;
            result[i] = -1;
        }
        
        for (int i = 0; i < word.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (result[j] == -1) {
                    if (word[i] == (char)arr[j]) {
                        result[j] = i;
                    }
                }
            }
        }
    
        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i] + " ");
        }
    }
    
    /*
     * 간단한 풀이
     *          
     *     Scanner sc = new Scanner(System.in);
        
        String word = sc.next();
        for (char c = 'a' ; c <= 'z' ; c++)
            System.out.print(word.indexOf(c) + " ");
     * 
     */
 
}
 
cs