프로그래머스/Level_0

[프로그래머스] Lv.0 - A로 B 만들기.py

ro-jun 2024. 9. 26. 22:55
728x90
반응형

문제

  • 문자열 before와 after가 매개변수로 주어질 때, before의 순서를 바꾸어 after를 만들 수 있으면 1을, 만들 수 없으면 0을 return 하도록 solution 함수를 완성해보세요.

제한사항

  • 0 < before의 길이 == after의 길이 < 1,000
  • before와 after는 모두 소문자로 이루어져 있습니다.

입출력 예시

  • 입출력 예 1
    • "olleh"의 순서를 바꾸면 "hello"를 만들 수 있습니다.
  • 입출력 예 2
    • "allpe"의 순서를 바꿔도 "apple"을 만들 수 없습니다.

코드 1

def solution(before, after):
    answer = 1
    ch_list = ['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']
    for ch in ch_list:
        if before.count(ch) != after.count(ch):
            answer = 0
    return answer

코드 2

def solution(before, after):
    answer = 1
    if sorted(before) != sorted(after):
        answer = 0
    return answer

출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges

728x90
반응형