Coding Test/프로그래머스

[프로그래머스] 모음사전 - 자바

lsh2613 2023. 8. 10. 23:20

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

 

프로그래머스

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

programmers.co.kr

문제 풀이

AEIOU는 각 알파벳을 숫자로 두고 6진수로 더욱 간단하게 풀 수 있지만 dfs가 약점인지라 연습겸 dfs로 풀었다. 더 효율적이고 간단한 풀이가 궁금하면 6진수로 풀이한 알고리즘을 찾아보면 좋을 것 같다.

 

dfs를 통해 가능한 모든 경우의 수에 대해 횟수를 카운트해서 MAP에 집어넣었다.

코드

import java.util.*;
class Solution {
    static Map<String, Integer> map = new HashMap<>();
    static String[] AEIOU={"A", "E", "I", "O", "U"};
    static int cnt;
    
    public int solution(String word) {
        dfs("",0);
        return map.get(word);
    }
    
    static void dfs(String str, int depth){
        map.put(str, cnt);
        
        if(depth==5) return;
        
        for(int i=0; i<AEIOU.length; i++){
            cnt++;
            dfs(str+AEIOU[i],depth+1);
        }
    }
}