[LeetCode][Python3] 79. Word Search
2019. 4. 13. 01:38 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/word-search/
My Solution :
class Solution:
def exist(self, board, word: str) -> bool:
R, C = len(board), len(board[0])
def dfs(row, col, word):
if not word:
return True
origin = board[row][col]
board[row][col] = '#'
for r, c in (
(row+1, col), (row-1, col),
(row, col+1), (row, col-1)):
if (0 <= r < R and 0 <= c < C and
board[r][c] == word[0] and dfs(r, c, word[1:])):
return True
board[row][col] = origin
for row in range(R):
for col in range(C):
if (board[row][col] == word[0] and
dfs(row, col, word[1:])):
return True
return False
Comment :
전형적인 백트래킹 (DFS) 문제이다. 고민 없이 바로 일반적인 패턴을 적용했고 쉽게 통과했다. 확실히 문제를 많이 풀어보니 유형에 익숙해지고 자신감이 생긴다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 322. Coin Change (0) | 2019.04.20 |
---|---|
[LeetCode][Python3] 54. Spiral Matrix (0) | 2019.04.18 |
[LeetCode][Python3] 76. Minimum Window Substring (0) | 2019.04.16 |
[LeetCode][Python3] 84. Largest Rectangle in Histogram (0) | 2019.04.15 |
[LeetCode][Python3] 313. Super Ugly Number (0) | 2019.04.13 |
[LeetCode][Python3] 218. The Skyline Problem (0) | 2019.04.11 |
[LeetCode][Python3] 150. Evaluate Reverse Polish Notation (0) | 2019.04.10 |
[LeetCode][Python3] 33. Search in Rotated Sorted Array (0) | 2019.04.10 |
최근에 달린 댓글 최근에 달린 댓글