Solution for LeetCode No. 79 works well in local interpreter but does not in submission
I'm solving No. 79 problem in LeetCode only to face with submission error.
An image below is for ur understand on my problem.
My code works completely well with all the test cases in local interpreter. However it does not in leetcode submission not passing even one single test case with the error of 'list index out of range'. I think this doesn't make sense because the problematic line that LeetCode pointed out is way simple that I must have not been confused with it.
Besides, if I submit the exactly same code on python3 submission rather than python, almost 70/80 test cases are passed - the submission occurs another error.
My code :
class Solution(object):
def exist(self, board, word):
############# dfs definition ###########
def dfs(i, j, word):
if (i < 0) or (i >= len(board)) or (j < 0) or (j >= len(board[0])) or self.visited_map[i][j] == 'o' or word[0] != board[i][j] :
return
self.visited_map[i][j] = 'o'
# if 'word' has no more character to search
if word[1:] == '':
self.res = True
return
dfs(i - 1, j, word[1:])
dfs(i + 1, j, word[1:])
dfs(i, j - 1, word[1:])
dfs(i, j + 1, word[1:])
########################################
m = len(board)
n = len(board[0])
self.res = False
for i in range(m):
for j in range(n):
if board[i][j] == word[0]:
self.visited_map = [['x' for i in range(n)] for j in range(m)]
dfs(i, j, word)
if self.res == True:
return True
return False
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
word = "ABCCED"
#board = [["C","A","A"],["A","A","A"],["B","C","D"]]
#word = "AAB"
Solution().exist(board, word)
This seemingly perfect code yields the unbeliveable 'list index out of range' in LeetCode, python.
With python 3, another error occurs. But I think this is not an important issue right now compared to the previous issue.
I hope you can help me with this issue !
※※※※
I've just realized that my code is not perfect as a solution. So now I get why I couldn't pass 10 or some test cases with python3. However, still cannot understand withe the first issue ('list index out of range')