https://school.programmers.co.kr/learn/courses/30/lessons/12916?language=java

1. 문자열에서 하나씩 문자를 뽑아 대소문자 판별
2. 대문자 -> 소문자 로 바꾼 후 result라는 문자열에 더한다.
3. isLowerCase() => 소문자면 true, 대문자면 false
class Solution {
boolean solution(String s) {
boolean answer = true;
String c = "";
int countP = 0;
int countY = 0;
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(Character.isUpperCase(ch)){
c += Character.toLowerCase(ch);
}else{
c += ch;
}
}
for(int j = 0; j < c.length(); j++){
char r = c.charAt(j);
if(r == 'p')
countP ++;
else if(r == 'y')
countY ++;
}
return countP == countY;
}
}
근데 변수랑 불필요한 식이 너무 많은거 같다..
class Solution {
boolean solution(String s) {
boolean answer = true;
s = s.toLowerCase();
int countP = 0;
int countY = 0;
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == 'p')
countP ++;
else if(s.charAt(i) == 'y')
countY ++;
}
return countP == countY;
}
}
toLowerCase()는 문자열을 한번에 모두 소문자로 변경해주는 함수였다 → 굳이 하나하나 비교해서 바꿀 필요가 없었던 것
=> 한번에 s를 모두 소문자로 변경해서 반복문 하나 제거
char ch 라는 변수가 굳이 필요 없을 거 같다
=> ch 변수 하나 제거