class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
def dfs(x: int, y: int, gold: int) -> None:
gold += grid[x][y]
nonlocal ans
ans = max(ans, gold)
rec = grid[x][y]
grid[x][y] = 0
for nx, ny in ((x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)):
if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] > 0:
dfs(nx, ny, gold)
grid[x][y] = rec
for i in range(m):
for j in range(n):
if grid[i][j] != 0:
dfs(i, j, 0)
return ans
class Solution:
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
abbr_equal_len = 0
num = 0
for c in abbr:
if c.isdigit() != 0:
if num == 0 and c == '0':
return False
num = num * 10 + int(c)
else:
abbr_equal_len += num
num = 0
abbr_equal_len += 1
if abbr_equal_len > len(word) or c != word[abbr_equal_len - 1]:
return False
abbr_equal_len += num
return abbr_equal_len == len(word)