Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- axios
- redux
- sw expert academy
- 이코테
- 리액트
- react-router
- 항해99
- C++
- 코딩테스트합격자되기
- java
- Get
- 알고리즘
- maeil-mail
- json-server
- redux-saga
- SW
- 항해플러스
- programmers
- 매일메일
- JavaScript
- 자바
- react
- useDispatch
- 프로그래머스
- createSlice
- react-redux
- redux-toolkit
- Python
- Algorithm
- 테코테코
Archives
- Today
- Total
Binary Journey
[프로그래머스] 문자열 내 p와 y의 개수 본문
반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
** Javascript
1) (오답임)
처음에 제출했던 코드인데 테스트케이스가 추가되면서 틀린 답이 되었다. p 와 y가 모두 없는 경우 때문이다.
function solution(s){
var string = s.toLowerCase();
if (!string.includes("p")) return false;
if (!string.includes("y")) return false;
return (string.match(/p/g) || []).length === (string.match(/y/g) || []).length;
}
2)
조건문만 삭제해주면 된다.
function solution(s){
var string = s.toLowerCase();
return (string.match(/p/g) || []).length === (string.match(/y/g) || []).length;
}
** Java
1)
import java.util.*;
class Solution {
boolean solution(String s) {
String[] strings = s.toLowerCase().split("");
int p = 0;
int y = 0;
for (String str : strings) {
if (str.equals("p")) p++;
if (str.equals("y")) y++;
}
return Objects.equals(p, y);
}
}
2)
import java.util.*;
class Solution {
boolean solution(String s) {
String[] strings = s.toLowerCase().split("");
int cnt = 0;
for (String str : strings) {
if (str.equals("p")) cnt++;
if (str.equals("y")) cnt--;
}
return Objects.equals(cnt, 0);
}
}
** Python
1)
def solution(s) :
cnt = 0
for i in s.lower() :
if i == "p" :
cnt += 1
if i == "y":
cnt -= 1
return cnt == 0
2) 다른 풀이 (추천 1등)
def solution(s) :
return s.lower().count('p') == s.lower().count('y')
반응형
'프로그래머스 > level 1' 카테고리의 다른 글
[프로그래머스] 다트게임 (0) | 2021.09.15 |
---|---|
[프로그래머스][위클리챌린지] 6주차 복서 정렬하기 (0) | 2021.09.12 |
[프로그래머스] 내적 (0) | 2021.09.01 |
[프로그래머스] 로또의 최고 순위와 최저 순위 (0) | 2021.09.01 |
[프로그래머스] 문자열 다루기 기본 (0) | 2021.08.30 |