|
| 1 | +from typing import List |
| 2 | +from unittest import TestCase, main |
| 3 | + |
| 4 | + |
| 5 | +class Solution: |
| 6 | + def exist(self, board: List[List[str]], word: str) -> bool: |
| 7 | + return self.solveWithDFS(board, word) |
| 8 | + |
| 9 | + """ |
| 10 | + Runtime: 5005 ms (Beats 27.48%) |
| 11 | + Time Complexity: O((MAX_R ** 2) * (MAX_C ** 2)), upper bound |
| 12 | + - 이중 for문 조회에 O(MAX_R * MAX_C) |
| 13 | + - node 하나당 조회하는 DIRS의 크기가 4이고, 최대 word의 길이만큼 반복하므로 O(4 * L) |
| 14 | + - 단 early return하므로 이는 upper bound |
| 15 | + > O(MAX_R * MAX_C) * O(4L) ~= O(MAX_R * MAX_C * L) |
| 16 | +
|
| 17 | + Memory: 16.59 MB (Beats 69.71%) |
| 18 | + Space Complexity: |
| 19 | + - MAX_R * MAX_C 격자의 칸마다 stack이 생성될 수 있으므로 O(MAX_R * MAX_C) |
| 20 | + - node의 크기는 visited에 지배적이고(curr_word 무시 가정), visited의 크기는 최대 L |
| 21 | + > O(MAX_R * MAX_C) * O(L) ~= O(MAX_R * MAX_C * L) |
| 22 | + """ |
| 23 | + def solveWithDFS(self, board: List[List[str]], word: str) -> bool: |
| 24 | + MAX_R, MAX_C, MAX_IDX = len(board), len(board[0]), len(word) |
| 25 | + DIRS = ((-1, 0), (1, 0), (0, -1), (0, 1)) |
| 26 | + |
| 27 | + for r in range(MAX_R): |
| 28 | + for c in range(MAX_C): |
| 29 | + if board[r][c] == word[0]: |
| 30 | + stack = [(r, c, 0, board[r][c], set([(r, c)]))] |
| 31 | + while stack: |
| 32 | + curr_r, curr_c, curr_idx, curr_word, curr_visited = stack.pop() |
| 33 | + |
| 34 | + if curr_word == word: |
| 35 | + return True |
| 36 | + |
| 37 | + for dir_r, dir_c in DIRS: |
| 38 | + post_r, post_c, post_idx = curr_r + dir_r, curr_c + dir_c, curr_idx + 1 |
| 39 | + if (post_r, post_c) in curr_visited: |
| 40 | + continue |
| 41 | + if not (0 <= post_r < MAX_R and 0 <= post_c < MAX_C): |
| 42 | + continue |
| 43 | + if not (post_idx < MAX_IDX and board[post_r][post_c] == word[post_idx]): |
| 44 | + continue |
| 45 | + |
| 46 | + post_visited = curr_visited.copy() |
| 47 | + post_visited.add((post_r, post_c)) |
| 48 | + stack.append((post_r, post_c, post_idx, curr_word + word[post_idx], post_visited)) |
| 49 | + |
| 50 | + return False |
| 51 | + |
| 52 | + |
| 53 | +class _LeetCodeTestCases(TestCase): |
| 54 | + def test_1(self): |
| 55 | + board = [["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]] |
| 56 | + word = "ABCCED" |
| 57 | + output = True |
| 58 | + self.assertEqual(Solution.exist(Solution(), board, word), output) |
| 59 | + |
| 60 | + def test_2(self): |
| 61 | + board = [["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]] |
| 62 | + word = "SEE" |
| 63 | + output = True |
| 64 | + self.assertEqual(Solution.exist(Solution(), board, word), output) |
| 65 | + |
| 66 | + def test_3(self): |
| 67 | + board = [["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]] |
| 68 | + word = "ABCB" |
| 69 | + output = False |
| 70 | + self.assertEqual(Solution.exist(Solution(), board, word), output) |
| 71 | + |
| 72 | + def test_4(self): |
| 73 | + board = [["A","B","C","E"],["S","F","E","S"],["A","D","E","E"]] |
| 74 | + word = "ABCESEEEFS" |
| 75 | + output = True |
| 76 | + self.assertEqual(Solution.exist(Solution(), board, word), output) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + main() |
0 commit comments