문제
프로그래머스 코딩테스트 연습 - 문자열 내 p와 y의 개수 (Level 1)
https://programmers.co.kr/learn/courses/30/lessons/12916
사용 알고리즘
- 문자열
풀이
문자열 내 p와 y의 개수를 세고 비교한다.
나의 코드
#include <string>
#include <iostream>
using namespace std;
bool solution(string s)
{
int cnt=0;
for(auto i:s) if(i=='p'||i=='P') cnt++;
for(auto i:s) if(i=='y'||i=='Y') cnt--;
return (cnt==0);
}
남의 코드(좋아요 최다 코드)
#include <string>
#include <iostream>
using namespace std;
bool solution(string s)
{
int p = 0;
int y = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'p' || s[i] == 'P')
p++;
else if (s[i] == 'y' || s[i] == 'Y')
y++;
}
return p == y;
}
728x90
'PS > Programmers' 카테고리의 다른 글
프로그래머스 코딩테스트 연습 - 신규 아이디 추천(Level 1) (0) | 2021.09.09 |
---|---|
프로그래머스 코딩테스트 연습 - x만큼 간격이 있는 n개의 숫자 (Level 1) (0) | 2021.08.24 |
프로그래머스 코딩테스트 연습 - 하샤드 수(Level 1) (0) | 2021.07.27 |
프로그래머스 코딩테스트 연습 - 핸드폰 번호 가리기 (Level 1) (0) | 2021.07.26 |
프로그래머스 코딩테스트 연습 - 약수의 합(Level 1) (0) | 2021.07.20 |