알고리즘, 코딩테스트/(JAVA)프로그래머스
(JAVA) 프로그래머스 문자열내 p와 y의 개수 - LEVEL1
momo0503
2022. 9. 27. 20:00
class Solution {
boolean solution(String s) {
boolean answer = true;
String s1 = s.toUpperCase();
int countP = 0;
int countY = 0;
for(int i=0;i<s1.length();i++){
if(s1.charAt(i) == 'P') countP++;
else if(s1.charAt(i) == 'Y') countY++;
}
System.out.println(" s1 : " +s1);
System.out.println(" countP : " +countP);
System.out.println(" countY : " +countY);
if(countP == 0 && countY == 0){
answer = true;
}else{
if(countP == countY){
answer = true;
}else{
answer = false;
}
}
return answer;
}
}
- 문제의 흐름대로 직관적으로 해결
- charAt(index) 는 String의 index위치의 값을 반환한다 ( return 타입 char )