์˜น์•Œ์ด

8/18/2025

https://school.programmers.co.kr/learn/courses/30/lessons/133499

function solution2(babbling) {
    var answer = 0;
    const arr = ["aya","ye","woo","ma"];
    const result = [];
    for(let i=0;i<babbling.length;i++){
    	let str = babbling[i];
    	// console.log(str);
    	for(const word of arr){
	    	if(str.includes(word)){
	    		// console.log(`${str} includes ${word}`)
	    		const bindex = str.indexOf(word);
	    		const eindex = str.indexOf(word)+word.length;
	    		str = str.substring(0,bindex)+str.substring(eindex);
	    	}
    	}
    	if(str===""){
  			result.push(babbling[i]);
    	}
    }
    // console.log(result);
    return result.length;
}

์ด๋ ‡๊ฒŒ ํ–ˆ๋”๋‹ˆ ์ •ํ™•๋„๊ฐ€ 55/100

์ดํ•ด๊ฐ€ ์•ˆ๋ผ์„œ ํด๋กœ๋“œํ•œํ…Œ ๋ฌผ์–ด๋ดค๋”๋‹ˆ ์ž๊ธฐ๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค๋ฉฐ ์–ด๋–ค ๊ฒฝ์šฐ์— ํ‹€๋ ธ๋Š”์ง€ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ์•Œ๋ ค๋‹ฌ๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๊ฑธ ์•Œ์•˜๋‹ค๋ฉด ์ฒ˜์Œ๋ถ€ํ„ฐ ๋งํ•ด์คฌ๊ฒ ์ง€..

๊ทธ๋ž˜์„œ ๋„ˆ๋ผ๋ฉด ์–ด๋–ป๊ฒŒ ํ’€๊ฑฐ๊ฐ™์€์ง€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋‹ฌ๋ผ๊ณ  ํ•ด์„œ ๊ทธ๊ฑธ ์ œ์ถœํ•ด๋ณด๋‹ˆ 100์  ํ†ต๊ณผ.

function solution(babbling) {
    const validSounds = ["aya", "ye", "woo", "ma"];
    let count = 0;
    
    for (const word of babbling) {
        if (canPronounce(word, validSounds)) {
            count++;
        }
    }
    
    return count;
}

function canPronounce(word, validSounds) {
    let i = 0;
    let prevSound = "";
    
    while (i < word.length) {
        let found = false;
        
        // ํ˜„์žฌ ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•˜๋Š” ์œ ํšจํ•œ ๋ฐœ์Œ ์ฐพ๊ธฐ
        for (const sound of validSounds) {
            if (word.startsWith(sound, i)) {
                // ์—ฐ์† ๋ฐœ์Œ ์ฒดํฌ
                if (sound === prevSound) {
                    return false;
                }
                
                prevSound = sound;
                i += sound.length;
                found = true;
                break;
            }
        }
        
        // ์–ด๋–ค ์œ ํšจํ•œ ๋ฐœ์Œ๋„ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด ๋ฐœ์Œ ๋ถˆ๊ฐ€๋Šฅ
        if (!found) {
            return false;
        }
    }
    
    return true;
}

๋น„๊ต

function solution2(babbling) {
    var answer = 0;
    const arr = ["aya","ye","woo","ma"];
    const result = [];
    for(let i=0;i<babbling.length;i++){
    	let str = babbling[i];
    	// console.log(str);
    	for(const word of arr){
	    	if(str.includes(word)){
	    		// console.log(`${str} includes ${word}`)
	    		const bindex = str.indexOf(word);
	    		const eindex = str.indexOf(word)+word.length;
	    		str = str.substring(0,bindex)+str.substring(eindex);
	    	}
    	}
    	if(str===""){
  			result.push(babbling[i]);
    	}
    }
    // console.log(result);
    return result.length;
}

๋‚ด ๋ฐฉ์‹์€ ๋ฐœ์Œํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๋“ค์˜ ๋ฐฐ์—ด arr์„ ๋งŒ๋“ค๊ณ ,
babbling์— ์žˆ๋Š” ๋‹จ์–ด๋“ค ํ•˜๋‚˜์”ฉ arr์˜ ์š”์†Œ๋“ค์„ ๊ฐ€์ง€๊ณ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์žˆ์œผ๋ฉด ๋บ€๋‹ค.

function solution(babbling) {
    const validSounds = ["aya", "ye", "woo", "ma"];
    let count = 0;
    
    for (const word of babbling) {
        if (canPronounce(word, validSounds)) {
            count++;
        }
    }
    
    return count;
}

function canPronounce(word, validSounds) {
    let i = 0;
    let prevSound = "";
    
    while (i < word.length) {
        let found = false;
        
        // ํ˜„์žฌ ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•˜๋Š” ์œ ํšจํ•œ ๋ฐœ์Œ ์ฐพ๊ธฐ
        for (const sound of validSounds) {
            if (word.startsWith(sound, i)) {
                // ์—ฐ์† ๋ฐœ์Œ ์ฒดํฌ
                if (sound === prevSound) {
                    return false;
                }
                
                prevSound = sound;
                i += sound.length;
                found = true;
                break;
            }
        }
        
        // ์–ด๋–ค ์œ ํšจํ•œ ๋ฐœ์Œ๋„ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด ๋ฐœ์Œ ๋ถˆ๊ฐ€๋Šฅ
        if (!found) {
            return false;
        }
    }
    
    return true;
}

ํด๋กœ๋“œ์˜ ๋ฐฉ์‹์€ ๋ฐœ์Œ ๊ฐ€๋Šฅํ•œ ๋‹จ์–ด๋“ค์ธ validSounds์—์„œ ๋ชจ๋“  ์š”์†Œ๋“ค์„ babbling์˜ ์š”์†Œ ํ•˜๋‚˜์”ฉ ๊ฒ€์ฆํ•˜๋Š”๊ฑด ๋‚˜์™€ ๊ฐ™๋‹ค.
๊ทธ๋Ÿฐ๋ฐ while (i < word.length)์กฐ๊ฑด ์•„๋ž˜ validSounds์˜ ์š”์†Œ๋“ค์„ ๊ณ„์† ๋ฐ˜๋ณตํ•œ๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  includes()๊ฐ€ ์•„๋‹ˆ๋ผ startsWith()๋กœ ๋น„๊ตํ•˜๋ฉด์„œ ์ˆœ์ฐจ์ ์œผ๋กœ i๋ฅผ ๋Š˜๋ ค๋‚˜๊ฐ„๋‹ค.

๊ฐ€์„ค

์œ„์— ๋น„๊ต๋ฅผ ์ ๋‹ค๋ณด๋‹ˆ๊นŒ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋– ์˜ฌ๋ž๋‹ค.
ํ˜น์‹œ ์ค‘๋ณต์‚ฌ์šฉ๋˜์ง€๋งŒ ๋ฐ˜๋ณต๋˜์ง€๋Š” ์•Š๋Š” ๋‹จ์–ด๊ฐ€ ๋ฌธ์ œ์ผ๊นŒ?
๋ฌธ์ œ์—์„œ๋Š” '๊ฐ™์€ ๋ฐœ์Œ์˜ ์—ฐ์†'์„ ํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ํ–ˆ์ง€ ์ค‘๋ณตํ•ด์„œ ์‚ฌ์šฉํ• ์ˆ˜ ์—†๋‹ค๊ณ ๋Š” ํ•œ ์  ์—†์œผ๋‹ˆ๊นŒ.
๊ฐ€๋ น "yemaye"๋Š” ๋‚ด solution2๋กœ๋Š” "yemaye"์—์„œ "ye"์™€ "ma"๋ฅผ ํ•œ๋ฒˆ์”ฉ ๋ฐ–์— ์ œ๊ฑฐํ•ด์ฃผ์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— "ye"๊ฐ€ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‚จ๊ฒŒ๋œ๋‹ค.

๊ทธ๋ž˜์„œ ๋‚ด๊ฐ€ ์ฒ˜์Œ์— ์ž‘์„ฑํ•œ solution2๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ๋ฒˆ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •ํ–ˆ๋‹ค.

function solution2(babbling) {
    var answer = 0;
    const validSounds = ["aya","ye","woo","ma"];

    const result = [];
    for(let i=0;i<babbling.length;i++){
    	let word = babbling[i];
    	// console.log(word);
    	let okay = true;
    	let prevSound = "";
    	while(word.length>0){
    		let flag = false;
    		for(const sound of validSounds){
    			if(word.startsWith(sound)){
    				if(sound === prevSound){
    					break;
    				}
    				word = word.substring(sound.length);
    				prevSound = sound;
    				flag = true;
    			}
    		}
    		if(!flag){
    			okay = false;
    			break;
    		}
    	}
    	if(okay){
    		result.push(babbling[i]);
    	}

    }
    // console.log(result);
    return result.length;
}

๋งค word๋ณ„๋กœ word.length>0 ์ธ ๋™์•ˆ ๊ณ„์†ํ•ด์„œ validSounds์˜ ์š”์†Œ๋“ค๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์„œ ๊ทธ๋ ‡๋‹ค๋ฉด sound์˜ ๊ธธ์ด๋งŒํผ word์˜ ์•ž๋ถ€๋ถ„์„ ์ž˜๋ผ์คฌ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ํด๋กœ๋“œ ๋‹ต์•ˆ์ฒ˜๋Ÿผ prevSound = sound๋ฅผ ํ•ด์ค˜์„œ ๊ฐ™์€ ๋ฐœ์Œ์ด ์—ฐ์†๋˜์ง€ ์•Š๊ฒŒ ์ฒ˜๋ฆฌํ–ˆ๋‹ค.
flag๋Š” validSounds๋ฅผ ํ•œ๋ฐ”ํ€ด ๋Œ๋ฉด์„œ ํ™•์ธํ–ˆ์„๋•Œ

  1. ์•„๋ฌด sound๋กœ ์‹œ์ž‘ํ•˜๋ฉด์„œ
  2. 2)sound===prevSound๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์— ์ฐธ์ด๋œ๋‹ค.
    validSounds๋ฅผ ํ•œ๋ฐ”ํ€ด ์ˆœํšŒํ–ˆ๋Š”๋ฐ flag๊ฐ€ false๋ผ๋ฉด ๋ฐ”๋กœ okay = false๋กœ ํ•˜๊ณ  ํ•ด๋‹น word๋Š” ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋‹ค์Œ word๋ฅผ ๊ฒ€์ฆํ•œ๋‹ค.
    word.length > 0 ์กฐ๊ฑด์ด ์žˆ์œผ๋‹ˆ๊นŒ ์„ฑ๊ณต์ ์œผ๋กœ ๋ฐœ์Œํ•  ์ˆ˜ ์žˆ๋Š” word์˜ ๊ฒฝ์šฐ word๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด ""์ด ๋  ๋•Œ ๊นŒ์ง€ flag===true ์ƒํƒœ์ด๊ณ  ๊ทธ๋Ÿฌ๋‹ˆ okay===ture์ƒํƒœ๋กœ ๋‚จ๊ณ  result์— ์ถ”๊ฐ€๋œ๋‹ค.