본문 바로가기
Study & Edu/Algorithm

[프로그래머스] 문자열 내 p와 y의 개수 (String to char array)

by 댓츠굿 2018. 9. 11.
문제)
대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.
예를들어 s가 pPoooyY면 true를 return하고 Pyy라면 false를 return합니다.

/* 문자열 내 p와 y의 개수
* URL: https://www.welcomekakao.com/learn/courses/30/lessons/12916
*/
class Solution15 {
boolean solution(String s) {
boolean answer = true;
char[] charArr = s.toCharArray();
int p = 0, y = 0;
for (int i=0; i<charArr.length; i++) {
String tmp = String.valueOf(charArr[i]).toLowerCase();
if("p".equals(tmp)) p++;
if("y".equals(tmp)) y++;
}
if(p != y) answer = false;
return answer;
}

}






My Github Address : https://github.com/boniato/algorithms/blob/master/programmers/Solution15.java



반응형