모음사전

108번뇌·2021년 9월 21일
post-thumbnail

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

import java.util.*;

class Solution {
    
    public static String[] alpha  = {"A","E","I","O","U"};
    public static ArrayList<String> list = new ArrayList<>();

    public static void dfs(String word, String makeWord, int depth)
    {
        if(5 == depth)
        {
            list.add(makeWord);
            return;
        }

        for(int i=0; i<5; i++)
        {
            if(!list.contains(makeWord))
            {
                list.add(makeWord);
            }
            dfs(word, makeWord+alpha[i], depth+1);
        }
    }
    
    public static int solution(String word) {
        int cnt = 0;
        dfs(word,"", 0);
        list.remove(0);

        for(var str : list) {
            cnt++;
            if (word.equals(str))
            {
                break;
            }
        }

        return cnt;
    }
}

익힐것 :
1. 보통의 dfs랑 다르게 chk이 없음
2. if(depth == 5)면 바로 add, 중간에 for문에선 contains체크 했음.

profile
내일 아침 눈을 떳을 때, '기대되는 오늘 하루를 만들기 위해' 나는 오늘도 생각하고 고민한다.

0개의 댓글