⏱소요 시간 - 20분
🔑해결 방법
String 클래스의 toLowerCase() / toUpperCase() / equalsIgnoreCase(str) 중에 하나를 골라 사용하면 된다.
대소문자를 통일시켜놓고, charAt(i)을 이용해 i의 값을 배열의 길이만큼 증가시키면서
p나 y가 존재하는지 체크하고 존재하면 cnt를 증가시키는 로직이다.
그리고 마지막에 두 cnt가 같은지 비교하면 된다.
🔎소스 코드
package step1;
// 문자열 내 p와 y의 개수
public class Ex15 {
public static void main(String[] args) {
System.out.println(solution("pPoooyY"));
System.out.println(solution("Pyy"));
System.out.println(solution("pppyy"));
}
public static boolean solution(String s) {
boolean answer = true;
s = s.toLowerCase();
int pCnt = 0;
int yCnt = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == 'p') {
pCnt++;
} else if(s.charAt(i) == 'y') {
yCnt++;
}
}
if(pCnt != yCnt) {
return false;
}
return answer;
}
}
'ALGORITHM' 카테고리의 다른 글
[프로그래머스 Java] 문자열 다루기 기본 (0) | 2021.01.13 |
---|---|
[프로그래머스 Java] 문자열 내림차순으로 배치하기 (0) | 2021.01.13 |
char[] <-> String <-> String[] (0) | 2021.01.12 |
[프로그래머스 Java] 문자열 내 마음대로 정렬하기 (0) | 2021.01.12 |
[프로그래머스 Java] 두 정수 사이의 합 (0) | 2021.01.12 |