본문 바로가기
알고리즘 & 자료구조/프로그래머스

모음 사전

by 신재권 2023. 2. 13.
package programmers;

public class 모음사전 {

   // A, E, I, O ,U 로만 만들 수 있는 5글자 이하의 단어
   // A, AA, ... , UUUUU
   // 주어진 단어가 몇번째 단어인지

   private static String[] dictionary = {"A", "E", "I", "O", "U"};
   private static int ans;

   public static int solution(String word) {
      ans = 0;
      go(word, 0, new StringBuilder());
      return ans;
   }

   private static void go(String word, int count, StringBuilder sb) {
      if (count == dictionary.length) {
         return;
      }
      for (int i = 0; i < 5; i++) {
         if (sb.toString().equals(word)) {
            return;
         }
         ans++;
         sb.append(dictionary[i]);
         go(word, count + 1, sb);
         if (sb.toString().equals(word)) {
            return;
         }
         sb.deleteCharAt(sb.length() - 1);
      }
   }

   public static void main(String[] args) {
      System.out.println(solution("A"));
      System.out.println(solution("AAAAE"));
      System.out.println(solution("AAAE"));
      System.out.println(solution("I"));
      System.out.println(solution("EIO"));
   }
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

2XN 타일링  (0) 2023.02.15
2개 이하로 다른 비트  (0) 2023.02.14
파일명 정렬  (0) 2023.02.12
프렌즈 4 블록  (0) 2023.02.11
방문 길이  (0) 2023.02.10