|
| 1 | +""" |
| 2 | +Solution: |
| 3 | + 1) board의 상하좌우를 탐색하되 아래 조건을 base case로 걸러준다. |
| 4 | + 1.1) index 가 word의 길이이면 결과값 판단 |
| 5 | + 1.2) out of bounds 판단 |
| 6 | + 1.3) index를 통해 현재 글자와 board의 글자의 일치 판단 |
| 7 | + 1.4) 방문 여부 판단 |
| 8 | + 2) board를 돌면서 backtrack 이 True 인 케이스가 있으면 return True |
| 9 | +
|
| 10 | +m = row_len |
| 11 | +n = col_len |
| 12 | +L = 단어 길이 |
| 13 | +Time: O(m n 4^L) |
| 14 | +Space: O(mn + L^2) = visit set O(mn) + 호출 스택 및 cur_word O(L^2) |
| 15 | +""" |
| 16 | + |
| 17 | + |
| 18 | +class Solution: |
| 19 | + def exist(self, board: List[List[str]], word: str) -> bool: |
| 20 | + ROWS, COLS = len(board), len(board[0]) |
| 21 | + visit = set() |
| 22 | + |
| 23 | + def backtrack(r, c, index, cur_word): |
| 24 | + if index == len(word): |
| 25 | + return word == cur_word |
| 26 | + if r < 0 or c < 0 or r == ROWS or c == COLS: |
| 27 | + return False |
| 28 | + if word[index] != board[r][c]: |
| 29 | + return False |
| 30 | + if (r, c) in visit: |
| 31 | + return False |
| 32 | + |
| 33 | + visit.add((r, c)) |
| 34 | + condition = ( |
| 35 | + backtrack(r + 1, c, index + 1, cur_word + board[r][c]) |
| 36 | + or backtrack(r - 1, c, index + 1, cur_word + board[r][c]) |
| 37 | + or backtrack(r, c + 1, index + 1, cur_word + board[r][c]) |
| 38 | + or backtrack(r, c - 1, index + 1, cur_word + board[r][c]) |
| 39 | + ) |
| 40 | + visit.remove((r, c)) |
| 41 | + return condition |
| 42 | + |
| 43 | + for i in range(ROWS): |
| 44 | + for j in range(COLS): |
| 45 | + if backtrack(i, j, 0, ""): |
| 46 | + return True |
| 47 | + return False |
| 48 | + |
| 49 | + |
| 50 | +""" |
| 51 | +Solution: |
| 52 | + 공간 복잡도 낭비를 줄이기 위해 cur_word 제거 |
| 53 | +Time: O(m n 4^L) |
| 54 | +Space: O(mn + L) |
| 55 | +""" |
| 56 | + |
| 57 | + |
| 58 | +class Solution: |
| 59 | + def exist(self, board: List[List[str]], word: str) -> bool: |
| 60 | + ROWS, COLS = len(board), len(board[0]) |
| 61 | + visit = set() |
| 62 | + |
| 63 | + def backtrack(r, c, index): |
| 64 | + if index == len(word): |
| 65 | + return True |
| 66 | + if r < 0 or c < 0 or r == ROWS or c == COLS: |
| 67 | + return False |
| 68 | + if word[index] != board[r][c]: |
| 69 | + return False |
| 70 | + if (r, c) in visit: |
| 71 | + return False |
| 72 | + |
| 73 | + visit.add((r, c)) |
| 74 | + condition = ( |
| 75 | + backtrack(r + 1, c, index + 1) |
| 76 | + or backtrack(r - 1, c, index + 1) |
| 77 | + or backtrack(r, c + 1, index + 1) |
| 78 | + or backtrack(r, c - 1, index + 1) |
| 79 | + ) |
| 80 | + visit.remove((r, c)) |
| 81 | + return condition |
| 82 | + |
| 83 | + for i in range(ROWS): |
| 84 | + for j in range(COLS): |
| 85 | + if backtrack(i, j, 0): |
| 86 | + return True |
| 87 | + return False |
| 88 | + |
| 89 | + |
| 90 | +""" |
| 91 | +Solution: |
| 92 | + 공간 복잡도를 줄이기 위해 visit set 제거 |
| 93 | + -> board[r][c]에 빈문자열을 잠깐 추가하는것으로 대체 |
| 94 | +Time: O(m n 4^L) |
| 95 | +Space: O(L) |
| 96 | +""" |
| 97 | + |
| 98 | + |
| 99 | +class Solution: |
| 100 | + def exist(self, board: List[List[str]], word: str) -> bool: |
| 101 | + ROWS, COLS = len(board), len(board[0]) |
| 102 | + |
| 103 | + def backtrack(r, c, index): |
| 104 | + if index == len(word): |
| 105 | + return True |
| 106 | + if r < 0 or c < 0 or r == ROWS or c == COLS: |
| 107 | + return False |
| 108 | + if word[index] != board[r][c]: |
| 109 | + return False |
| 110 | + |
| 111 | + temp = board[r][c] |
| 112 | + board[r][c] = "" |
| 113 | + condition = ( |
| 114 | + backtrack(r + 1, c, index + 1) |
| 115 | + or backtrack(r - 1, c, index + 1) |
| 116 | + or backtrack(r, c + 1, index + 1) |
| 117 | + or backtrack(r, c - 1, index + 1) |
| 118 | + ) |
| 119 | + board[r][c] = temp |
| 120 | + return condition |
| 121 | + |
| 122 | + for i in range(ROWS): |
| 123 | + for j in range(COLS): |
| 124 | + if backtrack(i, j, 0): |
| 125 | + return True |
| 126 | + return False |
0 commit comments