-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValidSudoku.java
49 lines (43 loc) · 1.23 KB
/
ValidSudoku.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.smlnskgmail.jaman.leetcodejava.medium;
// https://leetcode.com/problems/valid-sudoku/
public class ValidSudoku {
private final char[][] input;
public ValidSudoku(char[][] input) {
this.input = input;
}
public boolean solution() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char curr = input[i][j];
if (curr != '.') {
if (!isValid(input, i, j, curr)) {
return false;
}
}
}
}
return true;
}
private boolean isValid(char[][] board, int r, int c, char num) {
for (int i = c + 1; i < 9; i++) {
if (board[r][i] == num) {
return false;
}
}
for (int i = r + 1; i < 9; i++) {
if (board[i][c] == num) {
return false;
}
}
int x = r - r % 3;
int y = c - c % 3;
for (int i = x; i < x + 3; i++) {
for (int j = y; j < y + 3; j++) {
if (board[i][j] == num && !(i == r && j == c)) {
return false;
}
}
}
return true;
}
}