Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions word-search/seungyeonchoi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
class Solution {
/**
For each cell in board, start with i = 0:
At cell (r, c), looking for word[i]:
1. out of the board? -> return false
2. already visited? -> return false
3. not equal to word[i]? -> return false
4. word[i] is the last letter -> return true
5. mark visited
6. explore the 4 adjacent cells for word[i+1]
-> if any of them succeeds, return true
7. unmark visited, so other paths can use this cell
8. return false
If any start cell succeeds -> true
If every start cell fails -> false
한 칸 (r, c)에 들어왔다. 지금 word[i]를 찾는 중이다.
1. 격자 밖인지 체크 → 밖이면 실패
2. 이미 방문한 곳인지 체크 → 방문했으면 실패
3. 타겟 글자와 동일한지 체크 → 다르면 실패
4. 마지막 글자였다면 → 성공
5. 방문 표시
6. 상하좌우로 word[i+1]을 찾으러 탐색 → 하나라도 성공하면 성공
7. 방문 표시 해제하고 실패 반환

격자의 모든 칸 (r, c)에서:
위 과정을 i = 0으로 시작 → 성공하면 true
전부 실패하면 false
Comment on lines +16 to +27

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

풀이 과정을 주석으로 남겨주셔서 리뷰 시에 코드를 파악하기 용이한 것 같습니다 !👍

*/
/**
Complexity
- time: O(m*n*3^L), m x n is the board size, L is the word length
- try every cell as a start (m*n)
- each step has at most 3 directions, since the previous cell is already visited
- the path is at most L long, so 3^L paths per start
- space: O(m*n + L), visited array + recursion stack
*/
public boolean exist(char[][] board, String word) {
boolean [][] visited = new boolean [board.length][board[0].length];
int index = 0;
for (int i=0; i<board.length;i++) {
for (int j=0;j<board[0].length;j++) {
if (backTracking(board, word, i, j, visited, index)) {
return true;
}
}
}
return false;
}

private boolean backTracking(char[][] board, String word, int row, int col, boolean[][] visited, int index){
if(row<0 || row >= visited.length) {
return false;
}
if(col<0 || col >= visited[0].length) {
return false;
}

if (visited[row][col]) {
return false;
}

if (board[row][col] != word.charAt(index)) {
return false;
}

if (index == word.length()-1) {
return true;
}

visited[row][col] = true;
if (backTracking(board, word, row-1, col, visited, index+1)) {
return true;
}
if (backTracking(board, word, row+1, col, visited, index+1)) {
return true;
}
if (backTracking(board, word, row, col-1, visited, index+1)) {
return true;
}
if (backTracking(board, word, row, col+1, visited, index+1)) {
return true;
}
Comment on lines +71 to +82

@parkhojeong parkhojeong Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반복문 활용해서 이런 형태로 개선할 수 있을 거 같아요.

for dx, dy in [(-1,0), (1,0), (0,-1), (0,1)]:
  if (backTracking(board, word, row+dx, col+dy, visited, index+1) return true

visited[row][col] = false;
Comment on lines +71 to +83

@hyedall hyedall Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

하나의 visited를 재사용하면서 방문 기록을 복구해 시간복잡도를 개선할 수 있다는 점을 배웠습니다!
상하좌우 탐색 시 방향 배열을 활용하여 중복되는 코드를 줄이는 방식도 고려해보면 좋을 것 같습니다 😊

return false;
}
}
Loading