상세 컨텐츠

본문 제목

옹알이 (2) (연습문제)

프로그래머스 코딩테스트 풀이

by 발발개발 2022. 10. 29. 16:20

본문

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

 

프로그래머스

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

programmers.co.kr

 

풀이

class Solution {
    public int solution(String[] babbling) {
        int cnt = 0;

        for (String str : babbling) {
            StringBuilder sb = new StringBuilder(str.replaceAll("aya", "aya,").replaceAll("ye", "ye,")
                    .replaceAll("woo", "woo,").replaceAll("ma", "ma,"));

            if (sb.charAt(sb.length() - 1) != ',') {
                continue;
            }

            String[] wordArr = sb.toString().split(",");
            int correct = 0;

            for (int i = 0; i < wordArr.length; i++) {
                String word = wordArr[i];

                if (word.equals("aya") || word.equals("ye") || word.equals("woo") || word.equals("ma")) {
                    if (i > 0) {
                        if (!word.equals(wordArr[i - 1])) {
                            correct++;
                        }
                    } else {
                        correct++;
                    }
                }
            }

            if (correct == wordArr.length) {
                cnt++;
            }
        }

        return cnt;
    }
}

관련글 더보기

댓글 영역