Skip to content

Commit ae9576a

Browse files
committed
seunggon:week7 medium.js
1 parent 500ff33 commit ae9576a

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

seunggon/week7/medium.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
return 0;
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

Comments
 (0)