티스토리 뷰
문제 설명
대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.
예를들어 s가
pPoooyY
면 true를 return하고
Pyy
라면 false를 return합니다.
제한사항
- 문자열 s의 길이 : 50 이하의 자연수
- 문자열 s는 알파벳으로만 이루어져 있습니다.
입출력 예
s | answer |
---|---|
pPoooyY |
true |
Pyy |
false |
Solution in Java
public class Main {
public static void main(String[] args) {
String s = "yypypypppy";
Solution sol = new Solution();
boolean answer = sol.solution(s);
System.out.println(answer); }
}
class Solution {
boolean solution(String s) {
boolean answer = true;
int count = 0;
int count2 = 0;
s = s.toLowerCase();
for(int i = 0;i<s.length(); i++ ){
if(s.charAt(i) == 'y')
{count++;}
//단순히 int variable 만 for loop로 카운트 올려서 비교로 사용!
if(s.charAt(i) == 'p')
{count2++;}
}
if(count == count2){
answer = true;
}else{
answer = false;
}
return answer;
}
}
|
cs |
Solution in JavaScript
let answer;
function Solution(prop) {
let p = 0;
let y = 0;
let str = prop.toLowerCase();
for( let i = 0; i < str.length; i++){
if(str.charAt(i) === 'p') {
p++;
} else if (str.charAt(i) === 'y'){
y++;
}
}
if(p == y) {
return answer = true;
} else {
return answer = false;
}
}
console.log(Solution("yyPypyppp")); // false
reference
https://programmers.co.kr
'Algorithms' 카테고리의 다른 글
두 정수 사이의 합 (0) | 2019.01.31 |
---|---|
<프로그래머스> 같은 숫자는 싫어 in Java (0) | 2019.01.26 |
<프로그래머스 알고리즘문제> 문자열 다루기 기본 (0) | 2019.01.21 |
<프로그래머스 알고리즘문제> 약수의 합 (0) | 2019.01.18 |
<프로그래머스 알고리즘문제> 서울에서 김서방 찾기 (0) | 2019.01.17 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- equals()
- HackerRank Algorithm
- string class in java
- 프로그래머스 알고리즘
- Javascript Algorithm
- hackerrank javascript
- substring()
- java
- code refactoring
- ... in Javascript
- easy algorithm
- compareTo()
- math.max
- 프로그래머스
- C++
- Object type casting
- easy javascript algorithm
- hackerrank javascript solution
- HashMap
- Collection Framework
- algorithm
- 프로그래머스 알고리즘문제
- spring boot application
- 알고리즘
- javascript
- repeat()
- rest parameter
- hackerrank
- math.abs
- hackerrank solution
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
글 보관함