Skip to content

Commit 376939f

Browse files
authored
Merge pull request #1441 from AlgorithmWithGod/ksinji
[20251118] PGM / Lv3 / 단어 변환 / 강신지
2 parents f4d35af + 5a81559 commit 376939f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
```java
2+
class Solution {
3+
public boolean[] visited;
4+
public int answer = 0;
5+
6+
public int solution(String begin, String target, String[] words) {
7+
visited = new boolean[words.length];
8+
dfs(begin, target, words, 0);
9+
10+
return answer;
11+
}
12+
13+
public void dfs(String cur, String target, String[] words, int cnt) {
14+
if (cur.equals(target)) {
15+
answer = cnt;
16+
return;
17+
}
18+
19+
for (int i = 0; i < words.length; i++) {
20+
if (visited[i]) continue;
21+
22+
if (canConvert(cur, words[i])) {
23+
visited[i] = true;
24+
dfs(words[i], target, words, cnt+1);
25+
visited[i] = false;
26+
}
27+
}
28+
}
29+
30+
public boolean canConvert(String from, String to) {
31+
int cnt = 0;
32+
for (int i = 0; i < from.length(); i++) {
33+
if (from.charAt(i) != to.charAt(i)) cnt++;
34+
}
35+
36+
if (cnt == 1) return true;
37+
return false;
38+
}
39+
40+
}
41+
```

0 commit comments

Comments
 (0)