Bug with LeetCode?!


  • 0
    R

    When I run the following test case as a custom test case, it returns true. When I submit the code, it gives wrong answer with the same test case and shows that my code returns false!!!

    ["FYCENRD","KLNFINU","AAARAHR","NDKLPNE","ALANSAP","OOGOTPN","HPOLANO"]
    "FRANCE"

    class Solution {
    public:
        
        bool exists(int x, int y, int posInWord, string word, vector<vector<char>>& board, vector<vector<bool>>& visited) {
            if(posInWord == word.size())
                return true;
            
            if(board[x][y] != word[posInWord] || visited[x][y])
                return false;
            
            if(posInWord == word.size() - 1)
                return true;
            
            visited[x][y] = true;
            
            for(int i = -1; i <= 1; i++) {
                for(int j = -1; j <= 1; j++){
                    if(abs(i) != abs(j) && x+i >= 0 && x+i < board.size() && y+j >= 0 && y+j < board[0].size()) {
                        if(exists(x+i, y+j, posInWord+1, word, board, visited)) {
                            return true;
                        }
                    }
                }
            }
            
            visited[x][y] = false;
            return false;
            
        }
        
        bool exist(vector<vector<char>>& board, string word) {
            if(board.size() == 0) return false;
            
            vector<vector<bool>> visited(board.size(), vector<bool>(false, board[0].size()));
            for(int i = 0; i < board.size(); i++) {
                for(int j = 0; j < board[0].size(); j++) {
                    if(exists(i, j, 0, word, board, visited)) {
                        return true;
                    }
                }
            }
            return false;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.