본문 바로가기
Backend/알고리즘

[프로그래머스] Lv.2 모음 사전

by 박상윤 2024. 5. 28.

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

 

프로그래머스

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

programmers.co.kr

 

알파벳의 조합 >> 완전 탐색으로 품(dfs)

 

import java.util.*;

class Solution {
    
    public int cnt = 0;
    public HashMap<String,Integer> map;
    
    public int solution(String word) {
        int answer = 0;
        
        map = new HashMap<>();
        
        dfs("",0);
        
        answer = map.get(word);
        
        return answer;
    }
    
    public void dfs(String word, int depth) {

        map.put(word,cnt);
        
        if(depth == 5) {
            return;
        }
        
        for(int i = 0; i < 5; i++) {
            cnt++;
            dfs(word + change(i), depth + 1);
        }
    }
    
    public String change(int n) {
        if(n == 0) {
            return "A";
        }else if(n == 1) {
            return "E";
        }else if(n == 2) {
            return "I";
        }else if(n == 3) {
            return "O";
        }
        
        return "U";
    }
}

 

결과