sameFrequency 문제풀이

이후띵·2022년 4월 1일
0

알고리즘

목록 보기
11/14

내 풀이

/*
 문제
	write a function called sameFrequency.
	Given two positive integers,
	find out if the two number have the
	same frequency of digits.

	Your solution MUST have the following complexities:
	Time: O(N)
*/

function sameFrequency(a, b) {
    let arrA = String(a)
        .split('')
        .map((num) => {
            return String(num);
        });
    let arrB = String(b)
        .split('')
        .map((num) => {
            return String(num);
        });
    return JSON.stringify(arrA.sort()) === JSON.stringify(arrB.sort());
}

console.log(sameFrequency(182, 281)); // true
console.log(sameFrequency(34, 14)); // false
console.log(sameFrequency(3589578, 5879385)); // true
console.log(sameFrequency(22, 222)); // false

조금 더 좋은 방법이 있을 것 같긴 한데, 일단 O(N)으로 풀었다.

profile
이후띵's 개발일지

0개의 댓글