저장을 습관화
프로그래머스 LV.0 문자 개수 세기 본문
프로그래머스 LV.0 문자 개수 세기
https://school.programmers.co.kr/learn/courses/30/lessons/181902
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 명
문자 개수 세기
2. 문제 설명
알파벳 대소문자로만 이루어진 문자열 my_string이 주어질 때, my_string에서 'A'의 개수, my_string에서 'B'의 개수,..., my_string에서 'Z'의 개수, my_string에서 'a'의 개수, my_string에서 'b'의 개수,..., my_string에서 'z'의 개수를 순서대로 담은 길이 52의 정수 배열을 return 하는 solution 함수를 작성해 주세요.
3. 제한 사항
- 1 ≤ my_string의 길이 ≤ 1,000
4. 예시
my_string | result |
"Programmers" | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0] |
5. 기본 제공 코드
function solution(my_string) {
var answer = [];
return answer;
}
6. 제출한 내 답
const solution = (my_string) => {
let answer = Array(52).fill(0);
let alphabet = [
...Array(26)
.fill()
.map((_, i) => String.fromCharCode(i + 65)),
...Array(26)
.fill()
.map((_, i) => String.fromCharCode(i + 97)),
];
my_string.split("").forEach((v) => {
answer[alphabet.indexOf(v)] += 1;
});
return answer;
};
6-2. VSC에 작성한 내용
const solution = (my_string) => {
let answer = Array(52).fill(0);
// let upperAlphabet = Array(26)
// .fill()
// .map((_, i) => String.fromCharCode(i + 65));
// let lowAlphabet = Array(26)
// .fill()
// .map((_, i) => String.fromCharCode(i + 97));
let alphabet = [
...Array(26)
.fill()
.map((_, i) => String.fromCharCode(i + 65)),
...Array(26)
.fill()
.map((_, i) => String.fromCharCode(i + 97)),
];
my_string.split("").forEach((v) => {
answer[alphabet.indexOf(v)] += 1;
});
return answer;
};
// 테스트
console.log(solution("Programmers"));
7. 특이사항
자바스크립트에서 유니코드를 사용하여 알파벳 배열을 만들기
기본 String.fromCharCode() 개념
console.log(String.fromCharCode(65)); // A
console.log(String.fromCharCode(97)); // a
console.log(String.fromCharCode(65, 66, 67)); // ABC
fromCharCode(유니코드 번호)를 입력하면 그와 일치하는 문자가 나오게된다.
유니코드 번호 65부터 90은 대문자 A부터 Z이며
유니코드 번호 97부터 122는 소문자 a부터 z이다.
이를 응용하여 아래와 같이 알파벳 배열을 만들 수 있다.
let upperAlphabet = Array(26)
.fill()
.map((_, i) => String.fromCharCode(i + 65)); // 유니코드 65 ~ 90
let lowAlphabet = Array(26)
.fill()
.map((_, i) => String.fromCharCode(i + 97)); // 유니코드 97 ~ 122
console.log(upperAlphabet);
// [
// 'A', 'B', 'C', 'D', 'E', 'F',
// 'G', 'H', 'I', 'J', 'K', 'L',
// 'M', 'N', 'O', 'P', 'Q', 'R',
// 'S', 'T', 'U', 'V', 'W', 'X',
// 'Y', 'Z'
// ]
console.log(lowAlphabet);
// [
// 'a', 'b', 'c', 'd', 'e', 'f',
// 'g', 'h', 'i', 'j', 'k', 'l',
// 'm', 'n', 'o', 'p', 'q', 'r',
// 's', 't', 'u', 'v', 'w', 'x',
// 'y', 'z'
// ]
console.log([...lowAlphabet, ...upperAlphabet]);
// [
// 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
// 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
// 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
// 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
// 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
// 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
// 'W', 'X', 'Y', 'Z'
// ]
8. 다른 사람이 작성한 답
8-1. 좋아요를 가장 많이 받은 풀이법
function solution(m) {
var answer = [];
let al = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
let a = [];
a.length = 52;
a.fill(0);
m.split("").map((n)=>{
a[al.indexOf(n)]+=1
})
return a;
}
'코딩 테스트 > 프로그래머스 - 자바스크립트' 카테고리의 다른 글
프로그래머스 LV.0 두 수의 합 (0) | 2023.09.30 |
---|---|
프로그래머스 LV.1 문자열 다루기 기본 (0) | 2023.09.29 |
프로그래머스 LV.0 문자열 계산하기 (0) | 2023.09.29 |
프로그래머스 LV.0 영어가 싫어요 (0) | 2023.09.28 |
프로그래머스 LV.1 부족한 금액 계산하기 (0) | 2023.09.28 |