/* 외계어 사전
* 알파벳이 담긴 배열 spell과 외계어 사전 dic이 매개변수로 주어집니다.
* spell에 담긴 알파벳을 한번씩만 모두 사용한 단어가 dic에 존재한다면 1,
* 존재하지 않는다면 2를 return
*
* spell dic result
* ["p", "o", "s"] ["sod", "eocd", "qixm", "adio", "soo"] 2
* ["z", "d", "x"] ["def", "dww", "dzx", "loveaw"] 1
* ["s", "o", "m", "d"] ["moos", "dzx", "smm", "sunmmo", "som"] 2
*/
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 | import java.util.regex.Pattern; public class programmer_0_84 { static String[] a1 = {"p", "o", "s"}; static String[] b1 = {"sod", "eocd", "qixm", "adio", "soo"}; static String[] a2 = {"z", "d", "x"}; static String[] b2 = {"def", "dww", "dzx", "loveaw"}; static String[] a3 = {"s", "o", "m", "d"}; static String[] b3 = {"moos", "dzx", "smm", "sunmmo", "som"}; public int solution(String[] spell, String[] dic) { int answer = 2; for(int i = 0; i < spell.length; i++){ for(int k = 0; k < dic.length; k++){ dic[k] = dic[k].replaceFirst(spell[i],"1"); if(dic[k].length() == spell.length && Pattern.matches("^[1-9]*$", dic[k])) answer = 1; } } /* 다른사람 풀이. for(int i=0;i<dic.length;i++){ int answer = 0; for(int j=0;j<spell.length;j++){ if(dic[i].contains(spell[j])) answer ++; } if(answer==spell.length) return 1; } return 2; */ return answer; } public static void main(String args[]){ programmer_0_84 t = new programmer_0_84(); System.out.println("---------------------------------------"); System.out.println("result = " + t.solution(a1,b1)); System.out.println("---------------------------------------"); System.out.println("result2 = " + t.solution(a2,b2)); System.out.println("---------------------------------------"); System.out.println("result3 = " + t.solution(a3,b3)); System.out.println("---------------------------------------"); } } | cs |