프로그래머스 - 옹알이 (2)

greenTea·2023년 8월 8일
0
class Solution {
    public int solution(String[] babbling) {
       String[] strs = new String[]{"aya", "ye", "woo", "ma"};
        return (int) Arrays.stream(babbling).filter(i -> {
            for (String str : strs) {
                if (i.contains(str.repeat(2)))
                    return false;
            }
            return true;
        }).map(i -> {
            for (String str : strs) {
                i = i.replace(str, " ");
            }
            return i;
        }).filter(i -> i.isBlank()).count();
    }
}

🧐레벨 1단계의 문제입니다. replace 사용할 경우에만 주의하신다면 무난하게 푸실 수 있습니다.
1. 먼저 연속으로 같은 단어가 오는 경우를 거르기 위해 i.contains(str.repeat(2))를 이용하여 걸러줍니다.
2. 이후 걸러진 상태에서 해당 단어들을 " "replace 해줍니다.
이 때 주의할 점이 있는데 만약 ""로 한다면 아래의 경우 문제가 발생합니다.

wyeoo
-> ye를 replace 하면 woo
-> woo를 repalce 하면 ""가 되어 통과

이를 방지하기 위해 replace 할 때 " "한 칸 띄워진 상태로 바꿔주시면 됩니다.
3. filter를 통해 빈 문자열만 개수를 세주시면 됩니다. 자바에서는 isBlank라는 메소드를 지원하기에 이를 사용하였습니다.

출처 : 프로그래머스 - 옹알이(2)

profile
greenTea입니다.

0개의 댓글