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
class Solution {
    static int[] visit; 
    static int ans = Integer.MAX_VALUE;
    static void DFS(String[] words, String begin, String target, int cnt) {
        if(begin.equals(target)){
           
            ans = Math.min(ans, cnt);
        }else {
            for(int i=0; i<words.length; i++) {
                int c = 0;
                if(visit[i] == 1continue;
                for(int j=0; j<begin.length(); j++) {
                    if( begin.charAt(j)!=words[i].charAt(j)) c+=1;
                }
                if(c==1 && visit[i] == 0) {
                    visit[i] = 1;
                    String temp = begin;
                    begin = words[i];
                   
                    DFS(words, begin, target, cnt+1);
                   
                    begin = temp;
                    visit[i] = 0;
                }
            }
        }
    }
    public int solution(String begin, String target, String[] words) {
       
        visit = new int[words.length];
        for(int i=0; i<words.length; i++) {
            int c = 0;
            for(int j=0; j<begin.length(); j++) {
                if(begin.charAt(j)!=words[i].charAt(j)) c+=1;
            }
            if(c==1 && visit[i] == 0) {
                visit[i] = 1;
                
                String temp = begin;
                begin = words[i];
                
                DFS(words, begin, target, 1);
                
                begin = temp;
                visit[i] = 0;
            }
        }
        if(ans == Integer.MAX_VALUE)
            ans = 0;
        return ans;
    }
}
cs

적당히 잘 백트래킹 쓰면 됐다.