2
public boolean validTicTacToe(String[] board) {3
// cnt number of X and O4
int x = cntNumber('X', board);5
// this check can be omitted, it can be covered in the second number check.9
int o = cntNumber('O', board);10
if (x < o || x > o + 1) {13
// if(x <3 ) true, no need to see winning15
// if x has won, but game doesnt stop16
if (x == o && hasWon('X', board)) {19
// if o has won, but game doesnt stop20
if (x > o && hasWon('O', board)) {27
private int cntNumber(char target, String[] board) {29
for (int i = 0; i < 3; i++) {30
for (int j = 0; j < 3; j++) {31
if (target == board[i].charAt(j)) {39
private boolean hasWon(char target, String[] board) {40
String toWin = Character.toString(target).repeat(3);41
for (int i = 0; i < 3; i++) {42
if (board[i].equals(toWin)) {46
for (int j = 0; j < 3; j++) {48
for (int i = 0; i < 3; i++) {49
col = col && target == board[i].charAt(j);58
// check diagonal. If center is not target, not possible to form diag win.59
if (target != board[1].charAt(1)) {63
boolean diagonal1 = target == board[0].charAt(0);64
// only proceed if the first letter match. Otherwise might get false positive66
if (target == board[2].charAt(2)) {71
boolean diagonal2 = target == board[0].charAt(2);73
if (target == board[2].charAt(0)) {