programmers/프로그래머스-A로 B 만들기-python

cosmos·2022년 10월 16일
0
post-thumbnail

문제

풀이

  • for 반복문으로 입력 문자를 각각 순회하면서 개별요소를 dictionary에 할당해 알파벳의 갯수를 구해 두 입력요소의 구성요소가 같은지 판별하면 된다.

코드

# https://school.programmers.co.kr/learn/courses/30/lessons/120886
# programmers, level0: A로 B 만들기, Python3
def solution(before: str, after: str) -> int:
    before_dict, after_dict = {}, {}

    # before의 길이 == after의 길이여서
    for index in range(len(before)):
        if before[index] not in before_dict:
            before_dict[before[index]] = 1
        else:
            before_dict[before[index]] += 1
        if after[index] not in after_dict:
            after_dict[after[index]] = 1
        else:
            after_dict[after[index]] += 1

    return 1 if before_dict == after_dict else 0


if __name__ == '__main__':
    print(solution("olleh", "hello"))  # 1
    print(solution("allpe", "apple"))  # 0

결과

출처 & 깃허브

programmers a로 b만들기
github

0개의 댓글