단어변환(java)

문딤·2022년 9월 30일
0

단어변환

https://school.programmers.co.kr/learn/courses/30/lessons/43163

소스 코드


class Solution {
    static boolean [] visited;
    static int value =1000;
    public int solution(String begin, String target, String[] words) {
    int answer = 0;
        
      visited = new boolean[words.length];
      dfs(begin,target,words,0);

      if(value == 1000){
         return 0;
      }
        answer =value;
        return answer;
    }
    static void dfs(String b , String t, String [] a, int depth){

        // 같으면 return
    if(b.equals(t)){
      value = Math.min(value,depth);
      return ;
    }
    
      for (int i = 0; i < a.length; i++) {
        if(!visited[i] && check(b,a[i])){
              visited[i]=true;
              dfs(a[i],t,a,depth+1);
              visited[i]=false;
        }
      }
    }

     static boolean check(String b, String t){
      int count =0;
       for (int i = 0; i <b.length() ; i++) {
         if(b.charAt(i) == t.charAt(i)){
           count++;
         }
       }
       if(count == b.length() -1){
         return true;
       }
       return false;
    }
}

생각할 것

단어 비교를 어떻게 할건지 판별
💨 단어를 한개씩 바꿔야하니깐 target을 찾기 위해서는 target 단어 길이보다 1만큼 작고 똑같은애를 찾아서 return 하면 다음차례에는 타겟을 return 하지 않을까?

참고

https://jisunshine.tistory.com/157

profile
풀스택개발자가 될래요

0개의 댓글