문제 풀이/기타

[백준] 10809 (알파벳 찾기)

interfacer_han 2023. 11. 16. 12:13

#1 알고리즘

 

#2 코드

#2-1 자바

import java.util.Scanner;
import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String originalString = sc.nextLine().trim().toLowerCase();
        sc.close();
        
        Character[] testCase = new Character[originalString.length()];
        for(int i = 0; i < testCase.length; i++) {
            testCase[i] = originalString.charAt(i);
        }

        Character[] alphabets = {'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'};
        HashMap<Character, Integer> firstLocationOfAlphabet = new HashMap<Character, Integer>();
        
        for(Character alphabet : alphabets) {
            firstLocationOfAlphabet.put(alphabet, -1);
        }
        
        for(int i = (testCase.length - 1); i >= 0; i--) {
            firstLocationOfAlphabet.put(testCase[i], i);
        }
        
        for(Character alphabet : alphabets) {
            System.out.print(firstLocationOfAlphabet.get(alphabet) + " ");
        }
    }
}

 

#2-2 코틀린

fun main() {
    val testCase = readln().trim().lowercase()

    val alphabets : Array<Char> = arrayOf('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')

    val firstLocationsOfAlphabet = HashMap<Char, Int>()

    for (alphabet : Char in alphabets) {
        firstLocationsOfAlphabet.put(alphabet, -1)
    }

    for(i : Int in (testCase.length - 1) downTo 0) {
        firstLocationsOfAlphabet.put(testCase[i], i)
    }

    for(alphabet : Char in alphabets) {
        print("${firstLocationsOfAlphabet.get(alphabet)} ")
    }
}

'문제 풀이 > 기타' 카테고리의 다른 글

[백준] 2920 (음계)  (0) 2023.11.22
[백준] 2884 (알람 시계)  (0) 2023.11.21
[백준] 2292 (벌집)  (0) 2023.11.10
[백준] 2231 (분해합)  (0) 2023.11.09
[백준] 1978 (소수 찾기)  (0) 2023.11.08