[프로그래머스 Lv1] 문자열 내 p와 y의 개수 Python

Gayoung Lee·2022년 5월 17일
0

Algorithm

목록 보기
18/39

문제

대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.

예를 들어 s가 "pPoooyY"면 true를 return하고 "Pyy"라면 false를 return합니다.

내 풀이

def solution(s):
    answer = True
    
    s=s.lower()
    
    cnt_p=s.count('p')
    cnt_y=s.count('y')
    
    if cnt_p==cnt_y:
        answer=True
    else:
        answer=False
    
    return answer

다른사람 풀이

def numPY(s):
    return s.lower().count('p') == s.lower().count('y')

문법 정리

  • 리스트 함수 중 count() 라는 집계 함수!!!...
profile
삽질하며 성장하는 gayoungee

0개의 댓글