post-custom-banner

๐Ÿงก๋ฌธ์ œ ์„ค๋ช…

๋จธ์“ฑ์ด๋Š” ํƒœ์–ด๋‚œ ์ง€ 11๊ฐœ์›” ๋œ ์กฐ์นด๋ฅผ ๋Œ๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ์นด๋Š” ์•„์ง "aya", "ye", "woo", "ma" ๋„ค ๊ฐ€์ง€ ๋ฐœ์Œ๊ณผ ๋„ค ๊ฐ€์ง€ ๋ฐœ์Œ์„ ์กฐํ•ฉํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฐœ์Œ๋ฐ–์— ํ•˜์ง€ ๋ชปํ•˜๊ณ  ์—ฐ์†ํ•ด์„œ ๊ฐ™์€ ๋ฐœ์Œ์„ ํ•˜๋Š” ๊ฒƒ์„ ์–ด๋ ค์›Œํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ๋ฐฐ์—ด babbling์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋จธ์“ฑ์ด์˜ ์กฐ์นด๊ฐ€ ๋ฐœ์Œํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


๐Ÿ’›์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค babbling์˜ ๊ธธ์ด โ‰ค 10
  • 1 โ‰ค babbling[i]์˜ ๊ธธ์ด โ‰ค 30
  • ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’š์ž…์ถœ๋ ฅ ์˜ˆ

babblingresult
["aya", "yee", "u", "maa"]1
["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]2

๐Ÿ’™์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ["aya", "yee", "u", "maa"]์—์„œ ๋ฐœ์Œํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ "aya"๋ฟ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ["ayaye", "uuuma", "yeye", "yemawoo", "ayaayaa"]์—์„œ ๋ฐœ์Œํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ "aya" + "ye" = "ayaye", "ye" + "ma" + "woo" = "yewoo"๋กœ 2๊ฐœ์ž…๋‹ˆ๋‹ค. "yeye"๋Š” ๊ฐ™์€ ๋ฐœ์Œ์ด ์—ฐ์†๋˜๋ฏ€๋กœ ๋ฐœ์Œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 2์„ returnํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’œ์œ ์˜์‚ฌํ•ญ

๋„ค ๊ฐ€์ง€๋ฅผ ๋ถ™์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฐœ์Œ ์ด์™ธ์—๋Š” ์–ด๋–ค ๋ฐœ์Œ๋„ ํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์œผ๋กœ ๊ทœ์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "woowo"๋Š” "woo"๋Š” ๋ฐœ์Œํ•  ์ˆ˜ ์žˆ์ง€๋งŒ "wo"๋ฅผ ๋ฐœ์Œํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•  ์ˆ˜ ์—†๋Š” ๋ฐœ์Œ์ž…๋‹ˆ๋‹ค.


๐ŸคŽ๋‚˜์˜ ํ’€์ด

function solution(babbling) {
    const canBab = ['aya', 'ye', 'woo', 'ma']
    let result = 0
    // ์ „์ฒด ๋ฌธ์ž ๋ฐฐ์—ด
    for(let i = 0 ; i < babbling.length ; i ++) {
        // ๊ฐ€๋Šฅํ•œ ์˜น์•Œ์ด ๋ฐฐ์—ด
        for(let j = 0 ; j < canBab.length ; j ++ ) {
            // ๋ฐœ์Œํ•ด์•ผ ํ•  ๋ฌธ์ž๊ฐ€ ๊ฐ€๋Šฅํ•œ ์˜น์•Œ์ด๋ฅผ ํฌํ•จํ•˜๋Š”๊ฐ€?
            if(babbling[i].includes(canBab[j])) {
                // ๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„์„ ์ œ๊ฑฐํ•œ ํ›„ j--์„ ํ•˜์—ฌ ์ „๋ถ€ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ
                // ์—ฐ์†๋˜์ง€ ์•Š์€ ๋ถ€๋ถ„๋งŒ
                if(babbling[i].slice(0,canBab[j].length) === babbling[i].slice(canBab[j].length,canBab[j].length*2)) break
                babbling[i] = babbling[i].replace(canBab[j],'')
                j--
            }
            // ์ „๋ถ€ ๋‹ค ๋ฐœ์Œํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด
            if(babbling[i].length === 0) {
                result++
                break
            }
        }
    }
    return result
}
profile
๋‚ด ์ง€์‹์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋‹ดํ•จ
post-custom-banner

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 12์›” 17์ผ

์•„๋ฆ„๋‹ค์šด ์ฝ”๋“œ์—์š”

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ