[백준] #10809 알파벳 찾기 python

2022. 12. 22. 11:05

https://www.acmicpc.net/problem/10809

 

10809번: 알파벳 찾기

각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출

www.acmicpc.net

 

조금 더 깔끔하게 풀 수 있는 방법을 알아보자.

 

 

num_l = input()

char_l = ['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']

cnt = 0
char_dict = {}
for i in num_l:
    if i in char_dict:
        cnt += 1
        continue
    char_dict[i] = cnt
    cnt += 1
for i in char_dict:
    for j in range(len(char_l)):
        if char_l[j] == i:
            char_l[j] = str(char_dict[i])
for i in range(len(char_l)):
    if not str(char_l[i]).isdigit():
        char_l[i] = str(-1)

print(' '.join(char_l))

BELATED ARTICLES

more