(문제풀이)

백준 10809 (Java) : 알파벳 찾기

cogito-new 2023. 12. 4. 20:15
import java.util.ArrayList;
import java.util.Scanner;

public class Main_10809 {
    static void Solution(String text, String[] alphabet) {
        // 알파벳 순번만큼 돌면서 확인
        String answer = new String();
        for(int i = 0 ; i<alphabet.length; i++){
            int count = 0;
            // 단어 개수만큼 돌면서 비교
            for(int j = 0; j<text.length(); j++){
                // 알파벳이 텍스트에 포함되어 있는 경우
                String splitText = Character.toString(text.charAt(j));
                if(alphabet[i].equals(splitText)){
                    answer += text.indexOf(splitText);
                    answer += " ";
                    count ++;
                    break;
                };
            };
            if(count == 0){
                answer += "-1 ";
            }
        };
        System.out.println(answer);
    };

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();

        String[] alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
                            "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
                            "u", "v", "w", "x", "y", "z"};

        Solution(text, alphabet);


    };

};

1. 알파벳 순서로 확인하면서 위치를 찾으므로 알파벳 배열을 생성해두고 하나씩 비교

2. 동일 케이스를 방지하기 위해서 count 변수를 선언하고, count 가 증가하면 포함된 단어, count 가 0 이면 포함되지 않았으므로 -1 추가

 

반응형