Algorithm/프로그래머스

[프로그래머스] 옹알이 (Java)

Carroti 2022. 10. 25. 15:17

 

문제

 

프로그래머스

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

programmers.co.kr

 

알고리즘

없음

 

풀이

같은 단어를 두 번 말할 경우 다음 단어로 넘어가고,

말할 수 있는 단어들을 모두 공백으로 replace한 후

공백만 남으면 전부 말할 수 있는 단어이므로 answer를 1 증가시킨다.

 

코드

class Solution {
    public int solution(String[] babbling) {
        String[] cans = {"aya", "ye", "woo", "ma"};
        String[] cants = {"ayaaya", "yeye", "woowoo", "mama"};
        
        int answer = 0;
        label1: for(String str: babbling) {
            for(String cant: cants)
                if(str.contains(cant)) continue label1;
            
            for(String can: cans)
                str = str.replace(can, "");
            
            if("".equals(str))
                answer++;
        }
        return answer;
    }
}