Baekjoon algorithm training

[백준] 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)} ")
    }
}

'Baekjoon algorithm training' 카테고리의 다른 글

[백준] 11650 (좌표 정렬하기)  (0) 2023.11.18
[백준] 10989 (수 정렬하기 3)  (0) 2023.11.17
[백준] 2751 (수 정렬하기 2)  (0) 2023.11.15
[백준] 2750 (수 정렬하기)  (0) 2023.11.11
[백준] 2292 (벌집)  (0) 2023.11.10