코딩테스트/Python
[프로그래머스] 문자열 내 p와 y의 개수
yo~og
2021. 7. 29. 19:19
문자열 안에 있는 p와 y의 개수를 비교하는 문제이다.
def solution(s):
from collections import Counter
a = Counter(s)
if a['y']+a['Y'] == a['p']+a['P']:
return True
else:
return False
counter을 사용하여 풀었다! 공부한걸 써먹자 ㅎㅎ!!
다른사람 코드
def numPY(s):
# 함수를 완성하세요
return s.lower().count('p') == s.lower().count('y')
Counter로 푼 사람들도 많았는데 ,, 이분은 count로 푸셨다. lower은 있었던걸 완전 까먹고있던 함수다ㅠㅠ
기억하자!!
1. s.count()
2. s.lower()