From a923a2aacde93da3f79e594a06eaf8b373065b56 Mon Sep 17 00:00:00 2001 From: SSUM <116950962+ssum21@users.noreply.github.com> Date: Fri, 7 Mar 2025 21:27:10 +0900 Subject: [PATCH] =?UTF-8?q?[level=203]=20Title:=20=EB=8B=A8=EC=96=B4=20?= =?UTF-8?q?=EB=B3=80=ED=99=98,=20Time:=200.44=20ms,=20Memory:=2010.2=20MB?= =?UTF-8?q?=20-BaekjoonHub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 프로그래머스/3/43163. 단어 변환/README.md | 73 ++++++++++++++++++++ 프로그래머스/3/43163. 단어 변환/단어 변환.py | 22 ++++++ 2 files changed, 95 insertions(+) create mode 100644 프로그래머스/3/43163. 단어 변환/README.md create mode 100644 프로그래머스/3/43163. 단어 변환/단어 변환.py diff --git a/프로그래머스/3/43163. 단어 변환/README.md b/프로그래머스/3/43163. 단어 변환/README.md new file mode 100644 index 0000000..d752600 --- /dev/null +++ b/프로그래머스/3/43163. 단어 변환/README.md @@ -0,0 +1,73 @@ +# [level 3] 단어 변환 - 43163 + +[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/43163) + +### 성능 요약 + +메모리: 10.2 MB, 시간: 0.44 ms + +### 구분 + +코딩테스트 연습 > 깊이/너비 우선 탐색(DFS/BFS) + +### 채점결과 + +정확성: 100.0
합계: 100.0 / 100.0 + +### 제출 일자 + +2025년 03월 07일 21:27:08 + +### 문제 설명 + +

두 개의 단어 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 함수를 작성해주세요.

+ +
제한사항
+ + + +
입출력 예
+ + + + + + + + + + + + + + + + + + + + + +
begintargetwordsreturn
"hit""cog"["hot", "dot", "dog", "lot", "log", "cog"]4
"hit""cog"["hot", "dot", "dog", "lot", "log"]0
+
입출력 예 설명
+ +

예제 #1
+문제에 나온 예와 같습니다.

+ +

예제 #2
+target인 "cog"는 words 안에 없기 때문에 변환할 수 없습니다.

+ + +> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges \ No newline at end of file diff --git a/프로그래머스/3/43163. 단어 변환/단어 변환.py b/프로그래머스/3/43163. 단어 변환/단어 변환.py new file mode 100644 index 0000000..4ac975b --- /dev/null +++ b/프로그래머스/3/43163. 단어 변환/단어 변환.py @@ -0,0 +1,22 @@ +from collections import deque + +def solution(begin, target, words): + len_words = len(words) + len_word = len(words[0]) + queue = deque([(begin, 0)]) + visited = set() + while queue: + pop_word, dist = queue.popleft() + if pop_word == target: + return dist + for word in words: + num = 0 + for i in range(len_word): + if(pop_word[i] != word[i]): + num += 1 + if num == 1 and word not in visited: + queue.append((word, dist+1)) + visited.add(word) + + answer = 0 + return answer \ No newline at end of file