[프로그래머스/파이썬] Level 1 문자열 내 p와 y의 개수

bye9·2021년 4월 13일
0

알고리즘(코테)

목록 보기
109/130

https://programmers.co.kr/learn/courses/30/lessons/12916


문제풀이

간단한 문제였다.

프로그래머스에서 찾은 더 간단한 코드는 문자열을 소문자로 다 바꿔주고 각각 p와 y개수를 구해, 바로 리턴해주었다.

소스코드

def solution(s):
    p_cnt,y_cnt=0,0
    for i in s:
        if i=="p" or i=="P":
            p_cnt+=1
        elif i=="y" or i=="Y":
            y_cnt+=1
        
    return p_cnt==y_cnt

더 간단한 코드

def numPY(s):
    # 함수를 완성하세요
    return s.lower().count('p') == s.lower().count('y')

0개의 댓글