๋ฌธ์ ๐ฝ
https://school.programmers.co.kr/learn/courses/30/lessons/131128
๋ค๋ฅธ์ฌ๋ ํ์ด ๐ฝ
function solution(X, Y) {
let result = '';
const numObj = {};
for (const char of X) {
numObj[char] = (numObj[char] || 0) + 1;
}
for (const char of Y) {
if (!numObj[char]) continue;
result += char;
numObj[char]--;
}
if (result === '') return '-1';
if (+result === 0) return '0';
return [...result]
.map((num) => +num)
.sort((a, b) => b - a)
.join('');
}
๋ดํ์ด๐ฝ
1. ์ฑ๊ณต โญ
function solution(X, Y) {
let sumString=''
for(let i=9; i>=0; --i){
let array1 =[...X].filter((val)=>{ return Number(val)===i})
let array2 =[...Y].filter((val)=>{ return Number(val)===i})
sumString+=String(i).repeat(Math.min(array1.length,array2.length))
}
if(sumString==='') return "-1"
if(Number(sumString)===0) return "0";
return sumString
}
}
๋๋์ ๐ฝ
๋ฌธ์์ด => ๋ฐฐ์ด๋ก ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ์ ๋ณต๊ธฐํด๋ณด์
const array = [...string]
const array = Array.from(string)
const array = string.split('')
๋ฌธ์์ด ๊ด๋ จ method
str.replace("a", "") => str์ด๋ผ๋ ๋ฌธ์์ด์์ ๊ฐ์ฅ ๊ฐ๊น์ด "a"๋ฅผ ์ฐพ์ ""๋บ ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํํ๋ค!
sort ๋ฉ์๋๋ ์๋ณธ๋ฐฐ์ด์๋ ์ํฅ์ ๋ผ์น๋ค