일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- LeetCode
- DWG
- 파이썬
- kaggle
- Regular Expression
- 시바견
- Python
- Decorator
- 밴픽
- Python Implementation
- attribute
- 프로그래머스
- 43. Multiply Strings
- 운영체제
- 컴퓨터의 구조
- concurrency
- iterator
- Class
- Python Code
- Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- 109. Convert Sorted List to Binary Search Tree
- Protocol
- 715. Range Module
- t1
- shiba
- Convert Sorted List to Binary Search Tree
- data science
- 30. Substring with Concatenation of All Words
- Generator
Archives
- Today
- Total
Scribbling
LeetCode: 79. Word Search 본문
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
n, m = len(board), len(board[0])
for i in range(n):
for j in range(m):
if self.dfs(board, i, j, word, 0):
return True
return False
def dfs(self, board, y, x, word, k):
if k == len(word):
return True
if y < 0 or y >= len(board) or x < 0 or x >= len(board[0]) or board[y][x] == '@':
return False
char = word[k]
if board[y][x] != char:
return False
board[y][x] = '@'
dy, dx = [0, 1, 0, -1], [1, 0, -1, 0]
for d in range(4):
next_y, next_x = y + dy[d], x + dx[d]
if self.dfs(board, next_y, next_x, word, k+1):
return True
board[y][x] = char
return False
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 84. Largest Rectangle in Histogram (0) | 2021.10.04 |
---|---|
LeetCode: 85. Maximal Rectangle (0) | 2021.10.04 |
LeetCode: 78. Subsets (0) | 2021.10.01 |
LeetCode: 76. Minimum Window Substring (0) | 2021.09.30 |
LeetCode: 77. Combinations (0) | 2021.09.28 |