Skip to content

Commit 3367e48

Browse files
committed
feat:PG_84512
1 parent ef50cc1 commit 3367e48

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/week01/Yoepee/PG_84512.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package week01.Yoepee;
2+
3+
import java.util.*;
4+
5+
public class PG_84512 {
6+
// 만들어질 수 있는 최대 단어 길이
7+
static int MAX_LENGTH = 5;
8+
// 모음 사전 만들어지는 목록
9+
// 빈문자 고려해서 index 계산하려고 "" 초기값으로 추가
10+
static List<String> list = new ArrayList<>(Arrays.asList(""));
11+
// 알파벳 모음 배열
12+
static char[] v = "AEIOU".toCharArray();
13+
14+
// 완전탐색을 통해 모음사전 순회
15+
public static void backtrack(StringBuffer path,int depth, String target){
16+
// 최대 글자수를 초과하면 종료
17+
if(depth > MAX_LENGTH) return;
18+
19+
String current = path.toString();
20+
// 단어의 길이가 1 이상이면 저장
21+
if (depth >= 1) list.add(current);
22+
// 현재 단어가 주어진 단어라면 순회 종료
23+
if (current.equals(target)) return;
24+
25+
for (char c: v){
26+
// 새로운 모음 추가
27+
path.append(c);
28+
// 신규 모음 기준으로 모음사전 재귀
29+
backtrack(path, depth+1, target);
30+
// 기존 값으로 초기화 시켜야지 sb가 제대로 동작함
31+
path.deleteCharAt(depth);
32+
}
33+
}
34+
35+
public static int solution(String word) {
36+
StringBuffer sb = new StringBuffer("");
37+
backtrack(sb, 0, word);
38+
39+
int answer = list.indexOf(word);
40+
return answer;
41+
}
42+
43+
public static void main(String[] args) {
44+
// 기댓값 : 6
45+
System.out.println(solution("AAAAE"));
46+
// 기댓값 : 10
47+
System.out.println(solution("AAAE"));
48+
// 기댓값 : 1189
49+
System.out.println(solution("EIO"));
50+
}
51+
}

0 commit comments

Comments
 (0)