옹알이 (2)

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

처음에 틀렸던 부분이 j = -1 이 부분인데 이전에는 j = 0으로 했다가 1부터 시작하게 되어 0을 체크하지 못해 틀리게 되었다.

이후에 j = -1로 바꾸고 나서 해결하게 되었다.

using System;

public class Solution {
    public int solution(string[] babbling) {
        int result = 0;
        string[] speech = new string[] { "aya", "ye", "woo", "ma" };
        
        for (int i = 0; i < babbling.Length; i++)
        {
            string word = babbling[i];
            string preStr = "";
            for (int j = 0; j < speech.Length; j++)
            {
                if (preStr != speech[j] && word.StartsWith(speech[j]))
                {
                    preStr = speech[j];
                    word = word.Remove(0, speech[j].Length);
                    if (word.Length == 0)
                    {
                        result++;
                        break;
                    }

                    j = -1;
                }
            }
        }
        return result;
    }
}

+ Recent posts