[PS] 99ν΄λ½ μ½ν μ€ν°λ 16μΌμ°¨ TIL (λͺ¨μμ¬μ )
νκ·Έ: 99ν΄λ½, PS, TIL, μμ νμ, μ½λ©ν μ€νΈμ€λΉ, νν΄99
μΉ΄ν κ³ λ¦¬: PS
λ¬Έμ
μ€λͺ
μνλ²³ λͺ¨μ 5κ°λ‘ λ§λ€ μ μλ λͺ¨λ μ‘°ν©μμ νΉμ λ¨μ΄κ° λͺλ²μ§Έ μΈλ±μ€μ μλμ§ λ°ννλ©΄ λλ€.
νμ΄
λ¨μνκ² λ¨μ΄ νλλ‘ λ§λ€ μ μλ μ‘°ν©λΆν° λ¨μ΄ λ€μ―κ°μ μ‘°ν©κΉμ§ λͺ¨λ κ³μ°ν΄ λ°°μ΄μ λ΄κ³ , λ°°μ΄μ μ¬μ μμΌλ‘ μ λ ¬νλ€. κ·Έ ν μ°Ύκ³ μ νλ λ¨μ΄μ μΈλ±μ€λ₯Ό λ°ννλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution {
public int solution(String word) {
int answer = 0;
String[] alphabet = {"A", "E", "I", "O", "U"};
String[] all =
new String[(int) (5 + Math.pow(5, 2) + Math.pow(5, 3) + Math.pow(5, 4) + Math.pow(5, 5))];
int index = 0;
for (int i = 0; i < 5; i++) {
all[index++] = alphabet[i];
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
all[index++] = alphabet[i] + alphabet[j];
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 5; k++) {
all[index++] = alphabet[i] + alphabet[j] + alphabet[k];
}
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
all[index++] = alphabet[i] + alphabet[j] + alphabet[k] + alphabet[l];
}
}
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
for (int m = 0; m < 5; m++) {
all[index++] = alphabet[i] + alphabet[j] + alphabet[k] + alphabet[l] + alphabet[m];
}
}
}
}
}
Arrays.sort(all);
for (int i = 0; i < all.length; i++) {
if (all[i].equals(word)) {
answer = i + 1;
break;
}
}
return answer;
}
}
μ΄λ κ² νμ΄λ ν΅κ³Όλ₯Ό νκΈ΄ νλλ° μκ°κ³Ό λ©λͺ¨λ¦¬ μ¬μ©μ΄ λ§λ€.
νμ΄ (dfs)
μ κ³Όμ μ κ²°κ΅ "A", "E", "I," "O", "U"
λ₯Ό κ³μν΄μ λ§λΆμ¬ λκ°λ€λ κ²μ μ μ μλ€. μ²μ ββ λΉ λ¬Έμμ΄μμ μΆλ°ν΄ κΈΈμ΄κ° 5κ° λ λ κΉμ§ λͺ¨μ λ°°μ΄μ μννλ©΄μ μ΄μ λ¬Έμμ΄μ λ§λΆμ΄λ κ³Όμ μ μ¬κ·ν¨μλ‘ κ΅¬ννλ€.
1
2
3
4
5
6
7
8
9
10
11
12
all =
[A, E, I, O, U,
AA, AE, AI, AO, AU,
EA, EE, EI, EO, EU,
IA, IE, II, IO, IU,
OA, OE, OI, OO, OU,
UA, UE, UI, UO, UU,
AAA, AAE, AAI, AAO, AAU,
AEA, AEE, AEI, AEO, AEU,
AIA, AIE, AII, AIO, AIU,
...
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.*;
class Solution {
static String[] alphabet = {"A", "E", "I", "O", "U"};
static String[] all =
new String[(int) (5 + Math.pow(5, 2) + Math.pow(5, 3) + Math.pow(5, 4) + Math.pow(5, 5))];
static int index = 0;
public int solution(String word) {
for (int i = 1; i <= 5; i++) {
dfs("", i, 0);
}
Arrays.sort(all);
return Arrays.binarySearch(all, word) + 1;
}
static void dfs(String word, int length, int depth) {
if (depth == length) {
all[index++] = word;
return;
}
for (String a : alphabet) {
dfs(word + a, length, depth + 1);
}
}
}
λκΈλ¨κΈ°κΈ°