Post

KDT Unity 5주차 팀 프로젝트

2024-01-10 TIL

알고리즘 문제

오늘의 알고리즘 문제 : 옹알이(2)

머쓱이는 태어난 지 11개월 된 조카를 돌보고 있습니다. 조카는 아직 “aya”, “ye”, “woo”, “ma” 네 가지 발음과 네 가지 발음을 조합해서 만들 수 있는 발음밖에 하지 못하고 연속해서 같은 발음을 하는 것을 어려워합니다. 문자열 배열 babbling이 매개변수로 주어질 때, 머쓱이의 조카가 발음할 수 있는 단어의 개수를 return하도록 solution 함수를 완성해주세요.

Desktop View

해결

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
public class Solution {
    public int solution(string[] babbling) {

        int count = 0;
        string[] sounds = new string[] { "aya", "ye", "woo", "ma" };

        foreach (string word in babbling)
        {
            string temp = word;
            string prevSound = "";
            bool isBabble = true;

            while (temp.Length > 0 && isBabble)
            {
                bool found = false;

                foreach (string sound in sounds)
                {
                    if (temp.StartsWith(sound) && sound != prevSound)
                    {
                        temp = temp.Substring(sound.Length);
                        prevSound = sound;
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    isBabble = false;
                }
            }

            if (isBabble && temp.Length == 0)
            {
                count++;
            }
        }

        return count;
    }
}

Desktop View

This post is licensed under CC BY 4.0 by the author.