# This question is easy, but it's test case is quite tricky.

• ``````public class Solution {
int m, n;
public void solve(char[][] board) {
if (board.length == 0 || board[0].length == 0) return;
m = board.length; n = board[0].length;
findEdgeZeros(board); // change edge 'O' to '.'
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j ++) {
if (board[i][j] == 'O') board[i][j] = 'X';
else if (board[i][j] == '.') board[i][j] = 'O';
}
}
}

private void findEdgeZeros(char[][] board) {
for (int i = 0; i < m; i++) {
if (board[i][0] == 'O') tmpChange(board, i, 0);
if (board[i][n - 1] == 'O') tmpChange(board, i, n-1);
}
for (int j = 0; j < n; j++) {
if (board[0][j] == 'O') tmpChange(board, 0, j);
if (board[m - 1][j] == 'O') tmpChange(board, m - 1, j);
}
}

int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
private void tmpChange(char[][] board, int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n) return;
if (board[i][j] == 'O') {
board[i][j] = '.';
}
// !!! To past the last test case, we have to use if statement like this: "i > 1 || j > 1"
for (int[] d : dir) {
if (i + d[0] < 1 || i + d[0] >= m - 1 || j + d[1] < 1 || j + d[1] >= n - 1 || board[i + d[0]][j + d[1]] != 'O') continue;
tmpChange(board, i + d[0], j + d[1]);
}
}
}
``````

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