파이썬 알고리즘 : 둘만의 암호

index

2024년 3월 1일 알고리즘 문제풀이

문제

난이도

Lv.1

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def solution(s, skip, index):
    answer = []
    alphabet = list('abcdefghijklmnopqrstuvwxyz')
    n = len(alphabet)
    
    def check(num,x):
        idx = alphabet.index(x)
        
        while num:
            idx += 1
            # 알파벳의 끝에 도달하면 맨 처음으로 보냄
            if idx == n:
                idx = 0
            result = alphabet[idx]
            # skip에 포함되지 않을때만 갯수를 셈
            if result not in skip:
                num -= 1
        return result
    arr = list(s)
    for i in arr:
        answer.append(check(index,i))
        
    return ''.join(answer)