We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 500ff33 commit ae9576aCopy full SHA for ae9576a
1 file changed
seunggon/week7/medium.js
@@ -0,0 +1,36 @@
1
+// 단어 변환 (Lv. 3)
2
+
3
+// https://school.programmers.co.kr/learn/courses/30/lessons/43163
4
5
+function solution(begin, target, words) {
6
+ if (!words.includes(target)) {
7
+ return 0;
8
+ }
9
10
+ const queue = [[begin, 0]];
11
+ const visited = new Set([begin]);
12
13
+ while (queue.length > 0) {
14
+ const [current, count] = queue.shift();
15
16
+ if (current === target) return count;
17
18
+ for (const word of words) {
19
+ if (!visited.has(word) && isConvertible(current, word)) {
20
+ visited.add(word);
21
+ queue.push([word, count + 1]);
22
23
24
25
26
27
+}
28
29
+function isConvertible(word1, word2) {
30
+ let count = 0;
31
+ for (let i = 0; i < word1.length; i++) {
32
+ if (word1[i] !== word2[i]) count++;
33
+ if (count > 1) return false;
34
35
+ return count === 1;
36
0 commit comments