|
| 1 | +## 1. Iteration - I |
| 2 | + |
| 3 | +::tabs-start |
| 4 | + |
| 5 | +```python |
| 6 | +class Solution: |
| 7 | + def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool: |
| 8 | + ROWS, COLS = len(board), len(board[0]) |
| 9 | + direction = [[1, 0], [-1, 0], [0, 1], [0, -1], |
| 10 | + [1, 1], [-1, -1], [1, -1], [-1, 1]] |
| 11 | + |
| 12 | + board[rMove][cMove] = color |
| 13 | + |
| 14 | + def legal(row, col, color, direc): |
| 15 | + dr, dc = direc |
| 16 | + row, col = row + dr, col + dc |
| 17 | + length = 1 |
| 18 | + |
| 19 | + while 0 <= row < ROWS and 0 <= col < COLS: |
| 20 | + length += 1 |
| 21 | + if board[row][col] == ".": |
| 22 | + return False |
| 23 | + if board[row][col] == color: |
| 24 | + return length >= 3 |
| 25 | + row, col = row + dr, col + dc |
| 26 | + return False |
| 27 | + |
| 28 | + for d in direction: |
| 29 | + if legal(rMove, cMove, color, d): |
| 30 | + return True |
| 31 | + return False |
| 32 | +``` |
| 33 | + |
| 34 | +```java |
| 35 | +public class Solution { |
| 36 | + public boolean checkMove(char[][] board, int rMove, int cMove, char color) { |
| 37 | + int ROWS = board.length, COLS = board[0].length; |
| 38 | + int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, |
| 39 | + {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; |
| 40 | + |
| 41 | + board[rMove][cMove] = color; |
| 42 | + |
| 43 | + for (int[] d : direction) { |
| 44 | + if (legal(board, rMove, cMove, color, d)) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + } |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + private boolean legal(char[][] board, int row, int col, char color, int[] direc) { |
| 52 | + int ROWS = board.length, COLS = board[0].length; |
| 53 | + int dr = direc[0], dc = direc[1]; |
| 54 | + row += dr; |
| 55 | + col += dc; |
| 56 | + int length = 1; |
| 57 | + |
| 58 | + while (row >= 0 && row < ROWS && col >= 0 && col < COLS) { |
| 59 | + length++; |
| 60 | + if (board[row][col] == '.') { |
| 61 | + return false; |
| 62 | + } |
| 63 | + if (board[row][col] == color) { |
| 64 | + return length >= 3; |
| 65 | + } |
| 66 | + row += dr; |
| 67 | + col += dc; |
| 68 | + } |
| 69 | + return false; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +```cpp |
| 75 | +class Solution { |
| 76 | +public: |
| 77 | + bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) { |
| 78 | + int ROWS = board.size(), COLS = board[0].size(); |
| 79 | + vector<vector<int>> direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, |
| 80 | + {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; |
| 81 | + |
| 82 | + board[rMove][cMove] = color; |
| 83 | + |
| 84 | + for (auto& d : direction) { |
| 85 | + if (legal(board, rMove, cMove, color, d)) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + } |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | +private: |
| 93 | + bool legal(vector<vector<char>>& board, int row, int col, char color, vector<int>& direc) { |
| 94 | + int ROWS = board.size(), COLS = board[0].size(); |
| 95 | + int dr = direc[0], dc = direc[1]; |
| 96 | + row += dr; |
| 97 | + col += dc; |
| 98 | + int length = 1; |
| 99 | + |
| 100 | + while (row >= 0 && row < ROWS && col >= 0 && col < COLS) { |
| 101 | + length++; |
| 102 | + if (board[row][col] == '.') { |
| 103 | + return false; |
| 104 | + } |
| 105 | + if (board[row][col] == color) { |
| 106 | + return length >= 3; |
| 107 | + } |
| 108 | + row += dr; |
| 109 | + col += dc; |
| 110 | + } |
| 111 | + return false; |
| 112 | + } |
| 113 | +}; |
| 114 | +``` |
| 115 | +
|
| 116 | +```javascript |
| 117 | +class Solution { |
| 118 | + /** |
| 119 | + * @param {character[][]} board |
| 120 | + * @param {number} rMove |
| 121 | + * @param {number} cMove |
| 122 | + * @param {character} color |
| 123 | + * @return {boolean} |
| 124 | + */ |
| 125 | + checkMove(board, rMove, cMove, color) { |
| 126 | + const ROWS = board.length, COLS = board[0].length; |
| 127 | + const direction = [[1, 0], [-1, 0], [0, 1], [0, -1], |
| 128 | + [1, 1], [-1, -1], [1, -1], [-1, 1]]; |
| 129 | +
|
| 130 | + board[rMove][cMove] = color; |
| 131 | +
|
| 132 | + const legal = (row, col, color, [dr, dc]) => { |
| 133 | + row += dr; |
| 134 | + col += dc; |
| 135 | + let length = 1; |
| 136 | +
|
| 137 | + while (row >= 0 && row < ROWS && col >= 0 && col < COLS) { |
| 138 | + length++; |
| 139 | + if (board[row][col] === ".") { |
| 140 | + return false; |
| 141 | + } |
| 142 | + if (board[row][col] === color) { |
| 143 | + return length >= 3; |
| 144 | + } |
| 145 | + row += dr; |
| 146 | + col += dc; |
| 147 | + } |
| 148 | + return false; |
| 149 | + }; |
| 150 | +
|
| 151 | + for (let d of direction) { |
| 152 | + if (legal(rMove, cMove, color, d)) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + } |
| 156 | + return false; |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +::tabs-end |
| 162 | + |
| 163 | +### Time & Space Complexity |
| 164 | + |
| 165 | +* Time complexity: $O(1)$ |
| 166 | +* Space complexity: $O(1)$ |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## 2. Iteration - II |
| 171 | + |
| 172 | +::tabs-start |
| 173 | + |
| 174 | +```python |
| 175 | +class Solution: |
| 176 | + def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool: |
| 177 | + ROWS, COLS = len(board), len(board[0]) |
| 178 | + direction = [0, 1, 0, -1, 0, 1, 1, -1, -1, 1] |
| 179 | + |
| 180 | + board[rMove][cMove] = color |
| 181 | + |
| 182 | + for d in range(9): |
| 183 | + length = 1 |
| 184 | + row, col = rMove, cMove |
| 185 | + while True: |
| 186 | + row += direction[d] |
| 187 | + col += direction[d + 1] |
| 188 | + |
| 189 | + if row < 0 or col < 0 or row >= ROWS or col >= COLS or board[row][col] == ".": |
| 190 | + break |
| 191 | + if board[row][col] == color: |
| 192 | + if length > 1: |
| 193 | + return True |
| 194 | + break |
| 195 | + length += 1 |
| 196 | + |
| 197 | + return False |
| 198 | +``` |
| 199 | + |
| 200 | +```java |
| 201 | +public class Solution { |
| 202 | + public boolean checkMove(char[][] board, int rMove, int cMove, char color) { |
| 203 | + int ROWS = board.length, COLS = board[0].length; |
| 204 | + int[] direction = {0, 1, 0, -1, 0, 1, 1, -1, -1, 1}; |
| 205 | + |
| 206 | + board[rMove][cMove] = color; |
| 207 | + |
| 208 | + for (int d = 0; d < 9; d++) { |
| 209 | + int row = rMove, col = cMove; |
| 210 | + for (int length = 1; ; ++length) { |
| 211 | + row += direction[d]; |
| 212 | + col += direction[d + 1]; |
| 213 | + |
| 214 | + if (row < 0 || col < 0 || row >= ROWS || col >= COLS || board[row][col] == '.') |
| 215 | + break; |
| 216 | + if (board[row][col] == color) { |
| 217 | + if (length > 1) |
| 218 | + return true; |
| 219 | + break; |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + return false; |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +``` |
| 228 | + |
| 229 | +```cpp |
| 230 | +class Solution { |
| 231 | +public: |
| 232 | + bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) { |
| 233 | + int ROWS = board.size(), COLS = board[0].size(); |
| 234 | + int direction[10] = {0, 1, 0, -1, 0, 1, 1, -1, -1, 1}; |
| 235 | + |
| 236 | + board[rMove][cMove] = color; |
| 237 | + |
| 238 | + for (int d = 0; d < 9; ++d) { |
| 239 | + int row = rMove, col = cMove; |
| 240 | + for (int length = 1; ; ++length) { |
| 241 | + row += direction[d]; |
| 242 | + col += direction[d + 1]; |
| 243 | + |
| 244 | + if (row < 0 || col < 0 || row >= ROWS || col >= COLS || board[row][col] == '.') |
| 245 | + break; |
| 246 | + if (board[row][col] == color) { |
| 247 | + if (length > 1) |
| 248 | + return true; |
| 249 | + break; |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + return false; |
| 254 | + } |
| 255 | +}; |
| 256 | +``` |
| 257 | + |
| 258 | +```javascript |
| 259 | +class Solution { |
| 260 | + /** |
| 261 | + * @param {character[][]} board |
| 262 | + * @param {number} rMove |
| 263 | + * @param {number} cMove |
| 264 | + * @param {character} color |
| 265 | + * @return {boolean} |
| 266 | + */ |
| 267 | + checkMove(board, rMove, cMove, color) { |
| 268 | + const ROWS = board.length, COLS = board[0].length; |
| 269 | + const direction = [0, 1, 0, -1, 0, 1, 1, -1, -1, 1]; |
| 270 | + |
| 271 | + board[rMove][cMove] = color; |
| 272 | + |
| 273 | + for (let d = 0; d < 9; d++) { |
| 274 | + let row = rMove, col = cMove; |
| 275 | + for (let length = 1; ; ++length) { |
| 276 | + row += direction[d]; |
| 277 | + col += direction[d + 1]; |
| 278 | + |
| 279 | + if (row < 0 || col < 0 || row >= ROWS || col >= COLS || board[row][col] === '.') |
| 280 | + break; |
| 281 | + if (board[row][col] === color) { |
| 282 | + if (length > 1) |
| 283 | + return true; |
| 284 | + break; |
| 285 | + } |
| 286 | + } |
| 287 | + } |
| 288 | + return false; |
| 289 | + } |
| 290 | +} |
| 291 | +``` |
| 292 | + |
| 293 | +::tabs-end |
| 294 | + |
| 295 | +### Time & Space Complexity |
| 296 | + |
| 297 | +* Time complexity: $O(1)$ |
| 298 | +* Space complexity: $O(1)$ |
0 commit comments