๐Ÿค ์€์ง€log ๐Ÿค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] x ์‚ฌ์ด์˜ ๊ฐœ์ˆ˜ ๋ณธ๋ฌธ

๐ŸŒˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] x ์‚ฌ์ด์˜ ๊ฐœ์ˆ˜

Eun_zii 2024. 3. 14. 12:53

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.0

- x ์‚ฌ์ด์˜ ๊ฐœ์ˆ˜

 

๐Ÿ’š ๋ฌธ์ œ์„ค๋ช…

  • ๋ฌธ์ž์—ด myString์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. myString์„ ๋ฌธ์ž "x"๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆด์„ ๋•Œ ๋‚˜๋ˆ ์ง„ ๋ฌธ์ž์—ด ๊ฐ๊ฐ์˜ ๊ธธ์ด๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•œ ๋ฐฐ์—ด์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

 

๐Ÿ’š ์ œํ•œ์กฐ๊ฑด

  • 1 ≤ myString์˜ ๊ธธ์ด ≤ 100,000
    • myString์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

 

๐Ÿ’š ํ’€์ด

function solution(myString) {
    const answer = [];
    const x = myString.split("x");
    for (let i = 0; i < x.length; i++) {
        answer.push(x[i].length);
    }
    return answer;
}

 

โœ… "oxooxoxxox" ๋ผ๋Š” myString ๋ฌธ์ž์—ด์ด ์žˆ๊ณ , "x"๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์„๋•Œ,

myString.split("x") ๋ฅผ ํ•ด์ฃผ๋ฉด [ 'o', 'oo', 'o', '', 'o', '' ] ์ด๊ณ , 

๋นˆ ๋ฐฐ์—ด answer์— x[i].length๋ฅผ push (push() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๋์— ํ•˜๋‚˜ ์ด์ƒ์˜ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๋ฐฐ์—ด์˜ ์ƒˆ๋กœ์šด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.)

ํ•ด์ค€ํ›„ return answer ํ•ด์ฃผ๋ฉด [1,2,1,0,1,0] ์œผ๋กœ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋งŒ return ๋œ๋‹ค.

 

728x90