๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

์•„๋‚˜๊ทธ๋žจ(ํšจ์œจ์„ฑ-ํ•ด์‰ฌ) ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/Algorithm (๊ธฐ์ดˆ๋ฌธ์ œํ’€์ด)

์•„๋‚˜๊ทธ๋žจ(ํšจ์œจ์„ฑ-ํ•ด์‰ฌ)

Daehyunii 2022. 9. 2. 17:49

๋ฌธ์ œ(์ถœ์ฒ˜ : ์ธํ”„๋Ÿฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ๊ฐ•์˜, ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ)

 

Anagram์ด๋ž€ ๋‘ ๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ์˜ ๋‚˜์—ด ์ˆœ์„œ๋ฅผ ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ ๊ตฌ์„ฑ์ด ์ผ์น˜ํ•˜๋ฉด ๋‘ ๋‹จ์–ด๋Š” ์•„ ๋‚˜๊ทธ๋žจ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด AbaAeCe ์™€ baeeACA ๋Š” ์•ŒํŒŒ๋ฒณ์„ ๋‚˜์—ด ์ˆœ์„œ๋Š” ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ ๊ตฌ์„ฑ์„ ์‚ดํŽด๋ณด๋ฉด A(2), a(1), b(1), C(1), e(2)๋กœ ์•ŒํŒŒ๋ฒณ๊ณผ ๊ทธ ๊ฐœ์ˆ˜๊ฐ€ ๋ชจ๋‘ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์–ด๋Š ํ•œ ๋‹จ์–ด๋ฅผ ์žฌ ๋ฐฐ์—ดํ•˜๋ฉด ์ƒ๋Œ€ํŽธ ๋‹จ์–ด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ์•„๋‚˜๊ทธ๋žจ์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋‘ ๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋‘ ๋‹จ์–ด๊ฐ€ ์•„๋‚˜๊ทธ๋žจ์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ ์š”. ์•„๋‚˜๊ทธ๋žจ ํŒ๋ณ„์‹œ ๋Œ€์†Œ๋ฌธ์ž๊ฐ€ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค.

 

โ–ฃ ์ž…๋ ฅ์„ค๋ช…
์ฒซ ์ค„์— ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด๊ฐ€ ์ž…๋ ฅ๋˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ค„์— ๋‘ ๋ฒˆ์งธ ๋‹จ์–ด๊ฐ€ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

โ–ฃ ์ถœ๋ ฅ์„ค๋ช…
๋‘ ๋‹จ์–ด๊ฐ€ ์•„๋‚˜๊ทธ๋žจ์ด๋ฉด “YES"๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์•„๋‹ˆ๋ฉด ”NO"๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

โ–ฃ ์ž…๋ ฅ์˜ˆ์ œ 1

AbaAeCe

baeeACA

 

โ–ฃ ์ถœ๋ ฅ์˜ˆ์ œ 1

YES

 

โ–ฃ ์ž…๋ ฅ์˜ˆ์ œ 2

abaCC
Caaab

 

โ–ฃ ์ถœ๋ ฅ์˜ˆ์ œ 2

NO

 

Tip

 

๋ฌธ์ œํ’€์ด

//๋ฌธ์ œ ์„ค๋ช…๋งŒ ๋“ฃ๊ณ  ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ •๋‹ต
function solution(str1, str2){
    let answer = "yes";
    let sH = new Map();

    for(let x of str1){
        if(sH.has(x))sH.set(x, sH.get(x)+1);
        else(sH.set(x,1));
    }
    console.log(sH);

    for(let y of str2){
        if(sH.has(y)) sH.set(y, sH.get(y)-1);
        else answer ='no';
        if(sH.get(y) < 0) answer = 'no';
    }

    return answer;
}

let s1 = "AbaAeCe";
let s2 = "baeeACA";
console.log(solution(s1,s2));

let s3 = 'abaCC';
let s4 = 'Caaab';
console.log(solution(s3,s4));