Publish:

νƒœκ·Έ: , , , , ,

μΉ΄ν…Œκ³ λ¦¬:

img_3.png

문제

문제 링크

img_3.png

μ„€λͺ…

μ•ŒνŒŒλ²³ λͺ¨μŒ 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;
  }
}

img_3.png μ΄λ ‡κ²Œ 풀어도 톡과λ₯Ό ν•˜κΈ΄ ν•˜λŠ”λ° μ‹œκ°„κ³Ό λ©”λͺ¨λ¦¬ μ‚¬μš©μ΄ λ§Žλ‹€.

풀이 (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);
    }
  }
}

img_3.png

λ°©λ¬Έν•΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€! λŒ“κΈ€,지적,ν”Όλ“œλ°± μ–Έμ œλ‚˜ ν™˜μ˜ν•©λ‹ˆλ‹€πŸ˜Š

λŒ“κΈ€λ‚¨κΈ°κΈ°