코딩 테스트/프로그래머스

문자열 내 p와 y의 개수

ornni 2024. 8. 12. 10:00
728x90
반응형

첫번째 코드

대소문자를 구분하지 않으므로 모두 소문자로 변경한 후에 알파벳을 비교한다.

전체 알파벳에 대하여 비교한 후, 개수가 일치한 경우 True, 아니면 False를 return 한다.

 

def solution(s):
    count_p = 0
    count_y = 0
    
    s = s.lower()
    
    for i in s:
        if i == 'p':
            count_p += 1
        elif i == 'y':
            count_y += 1

    if count_p == count_y:
        return True
    else:
        return False

 

통과!


링크

https://github.com/ornni/programmers/tree/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/1/12916.%E2%80%85%EB%AC%B8%EC%9E%90%EC%97%B4%E2%80%85%EB%82%B4%E2%80%85p%EC%99%80%E2%80%85y%EC%9D%98%E2%80%85%EA%B0%9C%EC%88%98

 

programmers/프로그래머스/1/12916. 문자열 내 p와 y의 개수 at main · ornni/programmers

repository for recording Programmers Algorithm problem solving - ornni/programmers

github.com

 

반응형