[ 프로그래머스 ] #133499 : 옹알이 (2) - JAVA

🔗옹알이 (2)

class Solution {
    static String [] words=new String[]{"aya","ye","woo","ma"};

    public int solution(String[] babbling) {
        int answer=0;
        for(String babby : babbling){
            if(canSpeak(babby,"")) answer++;
        }

        return answer;
    }
    private boolean canSpeak(String str,String before){
        boolean result=false;

        if(str.isEmpty()) return true;

        for(int i=0;i<words.length;i++){
            if(str.startsWith(words[i])&& !words[i].equals(before)){
                str=str.substring(words[i].length(),str.length());
                result=canSpeak(str,words[i]);
            }
        }
        return result;
    }
}