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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์„ธ ๊ฐœ์˜ ๊ตฌ๋ถ„์ž ๋ณธ๋ฌธ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์„ธ ๊ฐœ์˜ ๊ตฌ๋ถ„์ž

Eun_zii 2024. 3. 29. 15:25

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

- ์„ธ ๊ฐœ์˜ ๊ตฌ๋ถ„์ž

 

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

  • ์ž„์˜์˜ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ฌธ์ž "a", "b", "c"๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด "baconlettucetomato"๋ผ๋ฉด ๋‚˜๋ˆ ์ง„ ๋ฌธ์ž์—ด ๋ชฉ๋ก์€ ["onlettu", "etom", "to"] ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
  • ๋ฌธ์ž์—ด myStr์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ์œ„ ์˜ˆ์‹œ์™€ ๊ฐ™์ด "a", "b", "c"๋ฅผ ์‚ฌ์šฉํ•ด ๋‚˜๋ˆ ์ง„ ๋ฌธ์ž์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•œ ๋ฐฐ์—ด์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.
  • ๋‹จ, ๋‘ ๊ตฌ๋ถ„์ž ์‚ฌ์ด์— ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ €์žฅํ•˜์ง€ ์•Š์œผ๋ฉฐ, returnํ•  ๋ฐฐ์—ด์ด ๋นˆ ๋ฐฐ์—ด์ด๋ผ๋ฉด ["EMPTY"]๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.

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

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

๐Ÿ’š ํ’€์ด

function solution(myStr) {
    const answer = myStr.split(/a|b|c/).filter(i => i !== "")
    return answer.length > 0 ? answer : ["EMPTY"];
}

 

โœ… 

๋ฌธ์ž์—ด myStr์„ ์ •๊ทœ์‹ /a|b|c/์— ๋”ฐ๋ผ ๋ถ„ํ• ํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ค€ํ›„, ๋นˆ ๋ฌธ์ž์—ด์„ ํ•„ํ„ฐ๋งํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ returnํ•ด์ค€๋‹ค.

๋ฐ˜ํ™˜ํ•  ๋ฐฐ์—ด์ด ๋น„์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ๋น„์–ด ์žˆ๋‹ค๋ฉด ["EMPTY"]๋ฅผ return ํ•ด์ค€๋‹ค.

 

์ •๊ทœ์‹ ์‚ฌ์šฉ์„ ํ•˜๊ณ ์‹ถ์ง€ ์•Š์•˜๋Š”๋ฐ split ์€ ๊ตฌ๋ถ„์ž๋ฅผ ํ•˜๋‚˜๋งŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— "a", "b", "c"๋ฅผ ๋™์‹œ์— ๊ตฌ๋ถ„์ž๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•˜๊ธฐ๋Š” ์–ด๋ ค์› ๋‹น.. ๊ทธ๋ž˜์„œ ์ •๊ทœ์‹ ์‚ฌ์šฉ ํ–ˆ๋‹ค๐Ÿ˜—๐Ÿ’จ (๋ฌผ๋ก  for๋ฌธ ์‚ฌ์šฉํ•ด์„œ ํ•˜๋Š”๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ์œผ๋กœ ํ•˜๊ณ ์‹ถ์—ˆ๋‹น ! @@)

728x90