public int solution(String[][] board, int h, int w) {
int count = 0;
String color = board[h][w];
int[][] directions = { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } };
for (int[] dir : directions) {
int tempH = h + dir[0];
int tempW = w + dir[1];
if (isValidMove(tempH, tempW, board)) {
if (color.equals(board[tempH][tempW])) {
count += 1;
}
}
}
return count;
}
private boolean isValidMove(int h, int w, String[][] board) {
return h >= 0 && h < board.length && w >= 0 && w < board[0].length;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/250125