IT/Java
[프로그래머스, Level 1] 문자열 내 p와 y의 개수(JAVA)
배당 줍는 다람쥐
2022. 8. 31. 20:37
반응형
안녕하세요.
배당 줍는 다람쥐입니다.
오늘 업로드하는 문제는 프로그래머스 Level 1, 문자열 내 p와 y의 개수입니다.
그러면 오늘도 문제 풀이 시작하겠습니다.
문제
https://school.programmers.co.kr/learn/courses/30/lessons/12916
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 내용은 링크 참고 부탁드립니다.
문제풀이
핵심포인트
- String.equalsIgnoreCase
Stream (Java Platform SE 8 )
A sequence of elements supporting sequential and parallel aggregate operations. The following example illustrates an aggregate operation using Stream and IntStream: int sum = widgets.stream() .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight())
docs.oracle.com
코드
import java.util.*;
class Solution {
boolean solution(String s) {
boolean answer = false;
int p_cnt = 0;
int y_cnt = 0;
String[] str = s.split("");
for (int i = 0; i < str.length; i++) {
if(str[i].equalsIgnoreCase("p")) p_cnt++;
else if(str[i].equalsIgnoreCase("y")) y_cnt++;
}
if(p_cnt == y_cnt) return true;
else return false;
}
}
반응형