Java | 알파벳 찾기[백준 10809]

나경호·2022년 4월 9일
0

알고리즘 Algorithm

목록 보기
58/106

알파벳 찾기 성공

출처 | 알파벳 찾기[백준 10809]

문제

알파벳 소문자로만 이루어진 단어 S가 주어진다. 각각의 알파벳에 대해서, 단어에 포함되어 있는 경우에는 처음 등장하는 위치를, 포함되어 있지 않은 경우에는 -1을 출력하는 프로그램을 작성하시오.

입력

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

출력

각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다.

만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출력한다. 단어의 첫 번째 글자는 0번째 위치이고, 두 번째 글자는 1번째 위치이다.


풀이

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

public class Main{

    public static void main(String[] args) throws IOException {
 
        BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
        
        int [] result = new int[26];
        for (int i = 0; i < 26; i++) {
            result[i] = -1;
        }
        String string = scan.readLine();

        for (int i = 0; i < string.length(); i++) {

            int index = Character.getNumericValue(string.charAt(i))-10;
            
            if (result[index] == -1) {
                result[index] = i;
            }
            
        }
        for (int i = 0; i < 26; i++) {
            System.out.print(result[i] + " ");
        }
        
        
    }   
}

// import java.io.*;

// public class Main
// {		
// 		public static void main(String[] args) throws IOException
// 		{
// 				BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
// 				String string = scan.readLine().trim();
// 				for(char c = 'a'; c<='z'; c++)
// 					System.out.print(string.indexOf(c)+" ");				
// 		}
// }

출처

  • 문제를 만든 사람: baekjoon
  • 잘못된 데이터를 찾은 사람: djm03178
  • 데이터를 추가한 사람: djm03178

알고리즘 분류

profile
기억창고👩‍🌾

0개의 댓글