-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path단어변환.js
47 lines (38 loc) · 923 Bytes
/
단어변환.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Solution 1
function isConvertible(word, newWord) {
let differenceCount = 0;
for (let i = 0; i < word.length; i++) {
if (word[i] !== newWord[i]) {
differenceCount += 1;
if (differenceCount > 1) {
return false;
}
}
}
return true;
}
function solution1(begin, target, words) {
const visited = Array.from({ length: words.length }, () => false);
let minCount = 0;
if (!words.includes(target)) {
return 0;
}
function dfs(word, l) {
if (word === target) {
minCount !== 0 ? (minCount = Math.min(minCount, l)) : (minCount = l);
}
if (l === words.length) {
return;
} else {
for (let i = 0; i < words.length; i++) {
if (!visited[i] && isConvertible(word, words[i])) {
visited[i] = true;
dfs(words[i], l + 1);
visited[i] = false;
}
}
}
}
dfs(begin, 0);
return minCount;
}