on
5주차 모음사전 (자바)
5주차 모음사전 (자바)
728x90
package prog; public class 오주차_모음사전 { public static void main(String[] args) { Solution_모음사전 s= new Solution_모음사전(); // System.out.println(s.solution("AAAAE")); // System.out.println(s.solution("AAAE")); System.out.println(s.solution("I")); // System.out.println(s.solution("EIO")); } } class Solution_모음사전{ static char[] M = new char[]{'A', 'E', 'I', 'O', 'U'}; static int count=0; static boolean check=false; static String collect; static int answer = 0; public int solution(String word) { answer = 0; collect = word; StringBuilder sb = new StringBuilder(); dfs(sb); return answer; } private void dfs(StringBuilder sb) { String tmpStr = sb.toString(); if (tmpStr.equals(collect)) { check=true; answer = count; return; } if(check || sb.length()>=5) return; for (int i = 0; i < M.length; i++) { count ++; dfs(sb.append(M[i])); sb.deleteCharAt(sb.toString().length()-1); } } }
728x90
from http://arch1tect.tistory.com/142 by ccl(A) rewrite - 2021-09-14 10:27:47