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
๋ฅผ ํ๋ฐํด ๋๋ฉด์ ํ์ธํ์๋
- ์๋ฌด
sound
๋ก ์์ํ๋ฉด์ - 2)
sound===prevSound
๊ฐ ์๋ ๊ฒฝ์ฐ์ ์ฐธ์ด๋๋ค.
validSounds
๋ฅผ ํ๋ฐํด ์ํํ๋๋ฐflag
๊ฐ false๋ผ๋ฉด ๋ฐ๋กokay = false
๋ก ํ๊ณ ํด๋นword
๋ ๊ฑด๋๋ฐ๊ณ ๋ค์word
๋ฅผ ๊ฒ์ฆํ๋ค.
word.length > 0
์กฐ๊ฑด์ด ์์ผ๋๊น ์ฑ๊ณต์ ์ผ๋ก ๋ฐ์ํ ์ ์๋word
์ ๊ฒฝ์ฐ word๊ฐ ๋น ๋ฌธ์์ด""
์ด ๋ ๋ ๊น์งflag===true
์ํ์ด๊ณ ๊ทธ๋ฌ๋okay===ture
์ํ๋ก ๋จ๊ณresult
์ ์ถ๊ฐ๋๋ค.