Skip to content

Commit 875acb2

Browse files
committed
[level 3] Title: 단어 변환, Time: 0.35 ms, Memory: 83.2 MB -BaekjoonHub
1 parent 1084b62 commit 875acb2

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

프로그래머스/3/43163. 단어 변환/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 85.2 MB, 시간: 0.63 ms
7+
메모리: 83.2 MB, 시간: 0.35 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 04월 28일 17:17:25
19+
2025년 05월 25일 00:33:38
2020

2121
### 문제 설명
2222

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
11
import java.util.*;
22
class Solution {
33
public int solution(String begin, String target, String[] words) {
4-
int level = bfs(begin,target,words);
4+
5+
int level = bfs(begin,target,words);
56
return level;
67
}
78

89
public int bfs(String begin, String target, String[] words){
9-
Queue<String> q = new LinkedList<>();
10-
boolean[] v = new boolean[words.length];
10+
Queue<String> q = new ArrayDeque<>();
11+
boolean[] v = new boolean[words.length];
1112
int level = 0;
1213
q.add(begin);
1314

1415
while(!q.isEmpty()){
15-
int size = q.size(); // 이번 레벨에 몇 개 노드 있는지
16-
for(int s = 0; s < size; s++) {
17-
String curVertex = q.remove();
16+
int size = q.size(); //이번 레벨에 존재하는 노드의 개수
17+
for(int i = 0; i < size; i++){
18+
String curVertex = q.poll(); //현재 노드 꺼내기
1819
if(curVertex.equals(target)){
1920
return level;
2021
}
21-
for(int i = 0; i < words.length; i++){
22-
if(!v[i] && getDiffCount(curVertex, words[i]) == 1){
23-
q.add(words[i]);
24-
v[i] = true;
25-
}
22+
23+
for(int j = 0; j < words.length; j++){
24+
if(!v[j] && getDiffCount(curVertex,words[j]) == 1){
25+
v[j] = true;
26+
q.add(words[j]);
2627
}
2728
}
28-
level++; // 이 레벨 size개 다 꺼내고 나서 level 1 올린다
2929
}
30-
31-
return 0;
30+
level ++;
3231
}
32+
return 0;
33+
}
34+
3335

34-
public int getDiffCount(String word, String target){
36+
public int getDiffCount(String word, String target ){
3537
int diffCount = 0;
36-
for(int i = 0; i < word.length(); i++){
38+
for (int i = 0; i < word.length(); i++){
3739
if(word.charAt(i) != target.charAt(i)){
38-
diffCount++;
40+
diffCount ++;
3941
}
4042
}
43+
4144
return diffCount;
4245
}
4346

47+
48+
4449
}

0 commit comments

Comments
 (0)