반응형
문제 보기
https://school.programmers.co.kr/learn/courses/30/lessons/84512
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
풀이
처음엔 수학적으로 풀고 싶었는데 하다 보니 머리가 아파서
살짝 야매인 것 같지만 DFS를 사용해 해결하였다.
private static void dfs(List<String> strings, char[] words, int depth, String cur) {
if(depth >= 5) {
return;
}
for (char c : words) {
String word = cur + c;
strings.add(word);
dfs(strings, words, depth + 1, word);
}
}
정말 사전을 직접 만들고 해당 단어의 인덱스가 몇 번일지 찾는 방식을 채택하였다.
전체 코드
import java.util.ArrayList;
import java.util.List;
class Solution {
public int solution(String word) {
char[] words = {'A', 'E', 'I', 'O', 'U'};
List<String> strings = new ArrayList();
dfs(strings, words, 0, "");
return strings.indexOf(word) + 1;
}
private static void dfs(List<String> strings, char[] words, int depth, String cur) {
if(depth >= 5) {
return;
}
for (char c : words) {
String word = cur + c;
strings.add(word);
dfs(strings, words, depth + 1, word);
}
}
}
GitHub 링크
CodingTest_AutoSave/프로그래머스/2/84512. 모음 사전 at main · MetroDefro/CodingTest_AutoSave
모든 코딩 테스트 자동 저장. Contribute to MetroDefro/CodingTest_AutoSave development by creating an account on GitHub.
github.com
반응형
'ProblemSolve > 항해99 코테스터디' 카테고리의 다른 글
99클럽 코테 스터디 8일차 TIL (미들러): [백준][Java] 2644 촌수계산 - 실버2 (0) | 2024.11.04 |
---|---|
99클럽 코테 스터디 7일차 TIL (챌린저): [백준][Java] 1240 노드사이의 거리 - 골드5 (0) | 2024.11.03 |
99클럽 코테 스터디 6일차 TIL (챌린저): [백준][Java] 2458 키 순서 - 골드4 (0) | 2024.11.02 |
99클럽 코테 스터디 5일차 TIL (챌린저): [백준][Java] 2457 공주님의 정원 - 골드3 (0) | 2024.11.01 |
99클럽 코테 스터디 5일차 TIL (미들러): [백준][Java] 24444 알고리즘 수업 - 너비 우선 탐색 1 - 실버2 (0) | 2024.11.01 |