일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 알고리즘
- 재귀함수
- OSI 7계층
- JavaScript Deep Dive
- 페이지네이션
- 버블정렬
- 자료구조
- 자바스크립트
- 정규표현식
- MUI
- CSS
- 백준
- 프론트엔드
- 코드스테이츠
- 코드스테이츠 메인프로젝트
- primitive type
- Node js
- 배열
- Native select
- 백준 nodeJS
- javascript
- 코딩테스트
- next/Image
- react js
- nodejs
- 유데미
- input class
- kakao map api
- 이벤트 루프
- sort
- Today
- Total
신입 개발자에서 시니어 개발자가 되기까지
두 수의 빈도수가 같은 지 판단하기 본문
문제
Write a function called sameFrequency. Given two positive integers, find out if the two numbers have the same frequency of digits.
Your solution MUST have the following complexities: (두 양의 정수를 인자로 받아서 각 수의 빈도가 같은 지 출력하라, 시간복잡도는 O(n)이어야 한다.)
ex ) 12134, 12341은 true / 1234, 2234는 false
코드
function sameFrequency(num1, num2) {
const obj = {};
for (let number of num1.toString()) {
obj[number] = (obj[number] || 0) + 1;
}
for (let number of num2.toString()) {
if (!obj[number]) {
return false;
}
obj[number] -= 1;
}
return true;
}
const result = sameFrequency(3589578, 5879385);
console.log(result);
/* refactoring
const strNum1 = num1.toString();
const strNum2 = num2.toString();
if(strNum1.length !== strNum2.length) return false;
해당 코드를 추가해서 length가 다르면 바로 false 반환하도록 한다.
*/
'javascript > [Udemy] algorithm & data structure' 카테고리의 다른 글
[Udemy 알고리즘&자료구조] 정렬(버블정렬) (0) | 2022.09.28 |
---|---|
[Udemy 알고리즘] 이진탐색 (1) | 2022.09.21 |
[다중포인터] 배열에서 고유의 값 카운트하기 (0) | 2022.09.12 |
Anagrams (1) | 2022.09.02 |
[section5] 문제해결패턴 (1) | 2022.08.30 |