단어변환

박상윤·2023년 8월 10일

가장 짧은 변화과정이므로
BFS 사용

import java.util.*;

class Solution {
static int answer = 0;
    public static int[] visited;

    public int solution(String begin, String target, String[] words) {
        int answer = 0;

        visited = new int[words.length];

        int ans_index = -1;
        for (int i = 0; i < words.length; i++) {
            if(target.equals(words[i])){
                ans_index = i;
            }
        }

        if(ans_index == -1){ // 정답이 없는 경우
            answer = 0;
        }else{ // 정답이 존재하는 경우
        	bfs(begin,words,target);
            answer = visited[ans_index];
        }

        return answer;
    }

    // 최소 몇단계? bfs
    public void bfs(String begin, String[] words, String target){

        Queue<Integer> queue = new LinkedList<>();

        for (int i = 0; i < words.length; i++) { // 첫 단어찾기
            if(getChange(begin,words[i])){
                visited[i] = 1;
                queue.add(i);

                if(words[i] == target){
                    return;
                }

            }
        }

        while(!queue.isEmpty()){
            int cur_idx = queue.poll();

            for (int j = 0; j < words.length; j++) { // 그다음 단어 찾기
                if(getChange(words[cur_idx], words[j]) && visited[j] == 0){
                    queue.add(j);
                    visited[j] = visited[cur_idx] + 1;

                    if(words[j] == target){
                        queue = new LinkedList<>(); // 초기화
                        break;
                    }
                }
            }
        }

    }

    public boolean getChange(String word, String newWord){
        int differentNum = 0;

        for (int i = 0; i < word.length(); i++) {
            if(word.charAt(i) != newWord.charAt(i)){
                differentNum++;
            }
        }

        return differentNum == 1 ? true : false;
    }
}

0개의 댓글