Contents

๋‹จ์–ด ๋ณ€ํ™˜ (with.Java)

   Oct 18, 2024     4 min read

๋‹จ์–ด ๋ณ€ํ™˜ (with.Java) ์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณธ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ, ํ’€์—ˆ๋˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํšŒ๊ณ ์™€ ๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋ฉฐ, ์•Œ์•„๊ฐ€๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ์— ๋Œ€ํ•ด ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ

๋‘ ๊ฐœ์˜ ๋‹จ์–ด begin, target๊ณผ ๋‹จ์–ด์˜ ์ง‘ํ•ฉ words๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์„ ์ด์šฉํ•˜์—ฌ begin์—์„œ target์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๋ณ€ํ™˜ ๊ณผ์ •์„ ์ฐพ์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

  1. ํ•œ ๋ฒˆ์— ํ•œ ๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ๋งŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. words์— ์žˆ๋Š” ๋‹จ์–ด๋กœ๋งŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด begin์ด โ€œhitโ€, target๊ฐ€ โ€œcogโ€, words๊ฐ€ [โ€œhotโ€,โ€dotโ€,โ€dogโ€,โ€lotโ€,โ€logโ€,โ€cogโ€]๋ผ๋ฉด โ€œhitโ€ -> โ€œhotโ€ -> โ€œdotโ€ -> โ€œdogโ€ -> โ€œcogโ€์™€ ๊ฐ™์ด 4๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๊ฐœ์˜ ๋‹จ์–ด begin, target๊ณผ ๋‹จ์–ด์˜ ์ง‘ํ•ฉ words๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ์†Œ ๋ช‡ ๋‹จ๊ณ„์˜ ๊ณผ์ •์„ ๊ฑฐ์ณ begin์„ target์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ๊ฐ ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 3 ์ด์ƒ 10 ์ดํ•˜์ด๋ฉฐ ๋ชจ๋“  ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • words์—๋Š” 3๊ฐœ ์ด์ƒ 50๊ฐœ ์ดํ•˜์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋ฉฐ ์ค‘๋ณต๋˜๋Š” ๋‹จ์–ด๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • begin๊ณผ target์€ ๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” 0๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

begintargetwordsreturn
โ€œhitโ€โ€œcogโ€[โ€œhotโ€, โ€œdotโ€, โ€œdogโ€, โ€œlotโ€, โ€œlogโ€, โ€œcogโ€]4
โ€œhitโ€โ€œcogโ€[โ€œhotโ€, โ€œdotโ€, โ€œdogโ€, โ€œlotโ€, โ€œlogโ€]0

๋ฌธ์ œ ํ’€์ด

import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;

class Solution {
    public int solution(String begin, String target, String[] words) {
        int answer = 0;
        Queue<String> que = new LinkedList<>();
        ArrayList<String> list = new ArrayList<>();
        int check = 0;
        for(String str : words){
            list.add(str);
        }
        if(!list.contains(target)){
            return 0;
        }

        que.offer(begin);
        list.remove(begin);

        while(!que.isEmpty()){
            for(int i = 0; i < que.size(); i++){
                String temp = "";
                String current_str = que.poll();
                if(current_str.equals(target)){
                    return answer;
                }
                for(String str : list){
                     if(convert(current_str, str)){
                         que.offer(str);
                         temp = str;
                     }
                }
                list.remove(temp);
            }
            answer++;
        }

        return 0;
    }

    public boolean convert(String word_1, String word_2){
        int count = 0;
        for(int i = 0; i < word_1.length(); i++){
            if(word_1.charAt(i) != word_2.charAt(i)){
                count++;
            }
        }

        return count == 1;
    }
}

ํ’€์ด ์„ค๋ช…

์ฃผ์–ด์ง„ ๋ฌธ์ œ๋Š” ์‹œ์ž‘ ๋ฌธ์ž์—ด begin์—์„œ ๋ชฉํ‘œ ๋ฌธ์ž์—ด target์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์†Œํ•œ์˜ ๋ณ€ํ™˜ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋‹จ, ๋ณ€ํ™˜ ๊ณผ์ •์—์„œ ๊ฐ ๋‹จ์–ด๋Š” ๋ฏธ๋ฆฌ ์ฃผ์–ด์ง„ ๋‹จ์–ด ๋ชฉ๋ก(words)์— ์กด์žฌํ•ด์•ผ ํ•˜๋ฉฐ, ๊ฐ ๋‹จ์–ด๋Š” ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ๋ฌธ์ž๋งŒ ๋ฐ”๋€” ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Queue์™€ ArrayList๋ฅผ ์‚ฌ์šฉํ•œ BFS๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

BFS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ begin ๋ฌธ์ž์—ด์—์„œ target ๋ฌธ์ž์—ด๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

๊ฐ ๋ณ€ํ™˜ ๋‹จ๊ณ„์—์„œ ํ˜„์žฌ ๋‹จ์–ด์™€ ๋‹ค์Œ ๋‹จ์–ด๊ฐ€ ํ•œ ๊ธ€์ž๋งŒ ๋‹ค๋ฅธ์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋ณ€ํ™˜ ๊ณผ์ •์—์„œ ๋ชฉํ‘œ ๋‹จ์–ด target์— ๋„๋‹ฌํ–ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ดˆ๊ธฐํ™” ๋‹จ๊ณ„์—์„œ Queue que๋Š” BFS๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ํ๋กœ ์‚ฌ์šฉ๋˜๊ณ , ArrayList list๋Š” ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•œ ๋‹จ์–ด ๋ชฉ๋ก์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

answer ๋ณ€์ˆ˜๋Š” ๋ณ€ํ™˜ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. words ๋ฐฐ์—ด์„ list๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  target ๋‹จ์–ด๊ฐ€ ๋ชฉ๋ก์— ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ target์ด ๋ชฉ๋ก์— ์—†์œผ๋ฉด ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

BFS ํƒ์ƒ‰ ๊ณผ์ •์—์„œ๋Š” ์‹œ์ž‘ ๋‹จ์–ด begin์„ ํ์— ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ์—์„œ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜์—ฌ ๊ฐ ๋ ˆ๋ฒจ์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

ํ˜„์žฌ ๋‹จ์–ด์™€ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•œ ๋‹จ์–ด๋ฅผ ์ฐพ์•„ ํ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€ํ™˜๋œ ๋‹จ์–ด๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ์ œ๊ฑฐํ•˜์—ฌ ์ค‘๋ณต ํƒ์ƒ‰์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.

๋ชฉํ‘œ ๋‹จ์–ด์— ๋„๋‹ฌํ•˜๋ฉด ๋ณ€ํ™˜ ํšŸ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

convert ๋ฉ”์†Œ๋“œ๋Š” ๋‘ ๋‹จ์–ด๊ฐ€ ํ•œ ๊ธ€์ž๋งŒ ๋‹ค๋ฅธ์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋‘ ๋‹จ์–ด์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ฐจ์ด๊ฐ€ 1์ธ ๊ฒฝ์šฐ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ true๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

์ฃผ์–ด์ง„ ์ œ์•ฝ ์กฐ๊ฑด ๋‚ด์—์„œ ์‹œ์ž‘ ๋ฌธ์ž์—ด์—์„œ ๋ชฉํ‘œ ๋ฌธ์ž์—ด๋กœ์˜ ์ตœ์†Œ ๋ณ€ํ™˜ ํšŸ์ˆ˜๋ฅผ BFS๋ฅผ ํ†ตํ•ด ํšจ๊ณผ์ ์œผ๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ณผ์ •์—์„œ ๋ณ€ํ™˜ ๊ฐ€๋Šฅ์„ฑ์„ ๊ฒ€์‚ฌํ•˜๊ณ , ๋ณ€ํ™˜๋œ ๋‹จ์–ด๋ฅผ ๊ด€๋ฆฌํ•˜์—ฌ ์ตœ์ ์˜ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.