class Solution: # @param board, a list of lists of 1 length string # @param word, a string # @return a boolean def exist(self, board, word): self.totalRow, self.totalCol = len(board), len(board[0]) for i in xrange(self.totalRow): for j in xrange(self.totalCol): if board[i][j] == word[0]: if self.dfs(board, i, j, word[1:]): return True return False def dfs(self, board, r, c, word): if len(word) == 0: return True increm=[[1,0],[-1,0],[0,1],[0,-1]] for [i,j] in increm: if self.totalRow>r+i>=0 and self.totalCol>c+j>=0 and board[r+i][c+j] == word[0]: ch, board[r][c] = board[r][c], '#' if self.dfs(board, r+i, c+j, word[1:]): return True board[r][c] = ch return False
Thursday, September 18, 2014
Leetcode: Word Search @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment