Time Limited Exceed...But why it works on my eclipse?? So strange...


  • 0
    W
    public class Solution {
        public boolean exist(char[][] board, String word) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            if(word.length() == 0) return true;
            if(board.length == 0 ) return false;
            int row = board.length;
            int column = board[0].length;
            char c = word.charAt(0);
            for(int i = 0; i < row; i++){
                for(int j = 0; j < column; j++){
                    if(c == board[i][j]){
                        board[i][j] = '.';
                        if(helper(board, i, j, word, 1)) return true;
                        board[i][j] = c; 
                    }
                }
            }
            return false;
        }
        private boolean helper(char[][] board, int i, int j, String word, int l){
            if(l == word.length()) return true;
            for(int k = i - 1; k <= i + 1; k += 2){
                if(k >= 0 && k < board.length && board[k][j] == word.charAt(l)){
                    board[k][j] = '.';
                    if(helper(board, k, j, word, l + 1)){
                        return true;
                    }
                    board[k][j] = word.charAt(l);
                }
            }
            for(int k = j - 1; k <= j + 1; j += 2){
                if(k >= 0 && k < board[i].length && board[i][k] == word.charAt(l)){
                    board[i][k] = '.';
                    if(helper(board, i, k, word, l + 1)){
                        return true;
                    }
                    board[i][k] = word.charAt(l);
                }
            }
            return false;
        }
    }
    

    Last executed input: ["a"], "ab"

    But it runs correctly on my own computer!
    Why?
    Thank you!!


Log in to reply
 

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