public class Solution {
public int solution(string[] babbling)
{
int answer = 0;
string[] s = { "aya", "ye", "woo", "ma" };
for (int i = 0; i < babbling.Length; i++)
{
string b = babbling[i];
int sCnt = 0;
bool isOne = true;
while (b != "")
{
if (b.Contains(s[sCnt]))
{
b = b.Replace(s[sCnt], "");
}
sCnt++;
if (sCnt >= 4)
{
if(!isOne)
{
break;
}
isOne = !isOne;
sCnt= 0;
}
}
if(b == "")
{
answer++;
}
}
return answer;
}
b 문자열을 replace 하는 과정에서 공백이 다 제거되어
'wyeoo' 문자열 같은 경우에는 'ye'가 제거 되면 'woo' 상태로 남아 옹알이로 말할 수 있는 말이 되어버린다.
replace 하는 과정에서 " " 공백을 하나 더해 하나의 문자로 인식하지 못하게 처리하였다.
public int solution(string[] babbling)
{
int answer = 0;
string[] s = { "aya", "ye", "woo", "ma" };
for (int i = 0; i < babbling.Length; i++)
{
string b = babbling[i];
int sCnt = 0;
bool isOne = true;
while (b != "")
{
if (b.Contains(s[sCnt]))
{
b = b.Replace(s[sCnt], " ");
}
sCnt++;
if (sCnt >= 4)
{
if(!isOne)
{
break;
}
isOne = !isOne;
sCnt= 0;
}
}
b = b.Replace(" ", "");
if(b == "")
{
answer++;
}
}
return answer;
}
정해진 옹알이 문자 순서로 붙여진 문자가 아니라 뒷 문자가 앞에 먼저 붙어 있을 수 도 있다.
(ex. "wooye")
그래서 isOne 이라는 bool 변수를 추가해서 두 번 순회하게 작업하였다.