대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.
예를 들어 s가 "pPoooyY"면 true를 return하고 "Pyy"라면 false를 return합니다.
문자열 s의 길이 : 50 이하의 자연수
문자열 s는 알파벳으로만 이루어져 있습니다.
class Solution {
boolean solution(String s) {
boolean answer = true;
// [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
System.out.println("Hello Java");
return answer;
}
}
//매개변수 s를 소문자로 바꿔서 변수 s2에 저장
//s2를 문자배열로 만들고 개수를 저장할 cnt1, cnt2를 생성 및 초기화
//반복문으로 'p'가 있으면 ++cnt1, 'y'가 있으면 ++cnt2
//cnt1==cnt2면 true 아니면 false
class Solution {
boolean solution(String s) {
boolean answer = true;
String s2 = s.toLowerCase();
char[] chArr = s2.toCharArray();
int cnt1=0;
int cnt2=0;
for(int i=0;i<chArr.length;i++) {
if(chArr[i]=='p') ++cnt1;
else if(chArr[i]=='y') ++cnt2;
}
if(cnt1==cnt2) return true;
return false;
}
}
//String s를 소문자로 해서 s에 저장
//count변수 초기화
//반복문으로 s의 문자가 p면 count++, y면 count--
//count가 0이면(p와y의 개수가 같으면) true, 아니면 false
class Solution {
boolean solution(String s) {
s = s.toLowerCase();
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'p')
count++;
else if (s.charAt(i) == 'y')
count--;
}
if (count == 0)
return true;
else
return false;
}
}