-
프로그래머스 - 문자열 내 p와 y의 개수 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:35
- 문제 링크 : 문자열 내 p와 y의 개수
정답
import java.util.*; class Solution { boolean solution(String s) { boolean answer = true; s = s.toLowerCase(); s = s.replaceAll("[^py]", ""); int p = 0; int y = 0; for(int i=0; i<s.length(); i++){ if(s.charAt(i) == 'p'){ p++; }else{ y++; } } if(p != y){ return false; } return answer; } }
분석
- 문자열 기능을 알고 있는지 물어보는 문제
참고할 만한 정답
class Solution { boolean solution(String s) { s = s.toUpperCase(); return s.chars().filter( e -> 'P'== e).count() == s.chars().filter( e -> 'Y'== e).count(); } }
비교 분석
- 와... String을 IntStream으로 변환하는 함수가 있었다.
- 문제풀이 후 다른 정답도 공부하면 좋은 이유다
- 문제풀이 후 다른 정답도 공부하면 좋은 이유다
- 새로 알게 된 내용이기는 하지만 공부했던 것처럼 wrapped type이 아닌 이상 성능 차이가 많이 발생한다.
- List, Set, Map 등 wrapper type을 사용하는 경우만 되도록 stream을 쓰자
반응형