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

Daehyunii's Dev-blog

๊ฐ€์žฅ ์งง์€ ๋ฌธ์ž๊ฑฐ๋ฆฌ(๋ฌธ์ž์—ด ํƒ์ƒ‰) ๋ณธ๋ฌธ

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

๊ฐ€์žฅ ์งง์€ ๋ฌธ์ž๊ฑฐ๋ฆฌ(๋ฌธ์ž์—ด ํƒ์ƒ‰)

Daehyunii 2022. 8. 31. 23:51

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

ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์—ด s์™€ ๋ฌธ์ž t๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋ฌธ์ž์—ด s์˜ ๊ฐ ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž t์™€ ๋–จ์–ด์ง„ ์ตœ์†Œ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

โ–ฃ ์ž…๋ ฅ์„ค๋ช…
์ฒซ ๋ฒˆ์งธ ์ค„์— ๋ฌธ์ž์—ด s์™€ ๋ฌธ์ž t๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

โ–ฃ ์ถœ๋ ฅ์„ค๋ช…
์ฒซ ๋ฒˆ์งธ ์ค„์— ๊ฐ ๋ฌธ์ž์—ด s์˜ ๊ฐ ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž t์™€ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

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

teachermode e

 

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

10121012210

 

Tip

 

๋ฌธ์ œํ’€์ด

//๊ฐ•์˜ ๋“ฃ๊ณ  ๋‚ด๊ฐ€ ๋‹ค์‹œ ์ž‘์„ฑํ•œ ๋‹ต
function solution(str, t){
    let result = [];
    let point = 1000;

    for(let i = 0 ; i < str.length ; i++){
        if(str[i] === t){
            point = 0;
            result.push(point);
        }else{
            point++;
            result.push(point);
        }
    }
    point = 1000;
    for(let j = str.length-1 ; j >= 0 ; j--){
        if(str[j] === t){
            point = 0;
        }else{
            point++;
            result[j] = Math.min(result[j], point);
        }
    }
    return result;
}


let str3="teachermode";
console.log(solution(str3, 'e'));