[프로그래머스] 둘만의 암호

단간단간·2024년 4월 5일
0

알고리즘 문제

목록 보기
47/106

문제 링크:

https://school.programmers.co.kr/learn/courses/30/lessons/155652

회고:

  • 문제를 끝까지 잘 읽어야 함.
  • skip에 포함되는 문자는 s에 포함되지 않는다는 조건을 못보고 풀어서 아래와 같이 구현했음.

python

def solution(s: str, skip:str, index:int) -> str:
    alphabets = "abcdefghijklmnopqrstuvwxyz"
    alphabet_index = {c: idx for idx, c in enumerate(alphabets)}
    index_alphabet = {idx: (c, False) if c in skip else (c, True) for idx, c in enumerate(alphabets)}

    result = []
    for c in s:
        count = 0
        idx = alphabet_index[c]
        while count != index:
            idx = (idx + 1) % 26

            next_c, is_valid = index_alphabet[idx]
            if is_valid:
                count += 1

        result.append(index_alphabet[idx][0])

    return "".join(result)


if __name__ == "__main__":
    result = solution("aukks", "wbqd", 5)

    print(result)
    print(result == "happy")
happy
True
profile
simple is best

1개의 댓글

comment-user-thumbnail
2024년 4월 21일

안녕하세요, 99클럽 그룹 리더 휴입니다!

회고를 작성하신 부분이 좋네요. 잘 읽고 갑니다.

앞으로도 힘내서 매일 TIL 도전해 보세요! 화이팅입니다 :)

==
99클럽 https://bit.ly/3TN5TBL

답글 달기