코딩공부/프로그래머스

[프로그래머스]Lv.1 옹알이 (2) C#

usingsystem 2023. 6. 14. 10:04
728x90

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

 

프로그래머스

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

programmers.co.kr

소스코드

          babbling[i] = babbling[i].Replace(str, "@").Replace(talk[j], " ");에서 마지막 Replace에서 string.empty가 아닌 " "공백 문자열을 넣는 이유는 빈문자열로 변경하면 정상이라는 결과가 나타난다. 만약 myeyea라는 입출력이 존재하고 yeye인 연속 문자열이 첫 번째 replace에서 빈문자열이되고 ma만 남기 때문에 다음 for문에서 ma는 하나만 존재하는 정상 범위이기 때문 이다.  

using System;

public class Solution {
    public int solution(string[] babbling) {
          int answer = 0;
            string[] talk = new string[] { "aya", "ye", "woo", "ma" };

            for (int i = 0; i < babbling.Length; i++)
            {
                for (int j = 0; j < talk.Length; j++)
                {
                    string str = talk[j] + talk[j];
                    babbling[i] = babbling[i].Replace(str, "@").Replace(talk[j], " ");
                }
                if (babbling[i].Trim().Length < 1)
                    answer++;
            }

            return answer;
    }
}
728x90