Skip to content

Commit 6e2463e

Browse files
committed
Runtime: 1 ms (Top 71.13%) | Memory: 41.6 MB (Top 42.96%)
1 parent 3d46e8a commit 6e2463e

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

scripts/algorithms/V/Valid Tic-Tac-Toe State/Valid Tic-Tac-Toe State.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
// Runtime: 1 ms (Top 71.13%) | Memory: 41.6 MB (Top 42.96%)
12
class Solution {
23
public boolean validTicTacToe(String[] board) {
3-
//cnt number of X and O
4+
//cnt number of X and O
45
int x = cntNumber('X', board);
5-
//this check can be omitted, it can be covered in the second number check.
6+
//this check can be omitted, it can be covered in the second number check.
67
if(x >5){
78
return false;
89
}
@@ -23,7 +24,7 @@ public boolean validTicTacToe(String[] board) {
2324
}
2425
return true;
2526
}
26-
27+
2728
private int cntNumber(char target, String[] board){
2829
int res = 0;
2930
for(int i = 0; i<3; i++) {
@@ -35,7 +36,7 @@ private int cntNumber(char target, String[] board){
3536
}
3637
return res;
3738
}
38-
39+
3940
private boolean hasWon(char target, String[] board){
4041
String toWin = Character.toString(target).repeat(3);
4142
for(int i = 0; i<3; i++) {
@@ -55,19 +56,19 @@ private boolean hasWon(char target, String[] board){
5556
return true;
5657
}
5758
}
58-
//check diagonal. If center is not target, not possible to form diag win.
59+
//check diagonal. If center is not target, not possible to form diag win.
5960
if(target != board[1].charAt(1)){
6061
return false;
6162
}
62-
63+
6364
boolean diagonal1 = target == board[0].charAt(0);
64-
//only proceed if the first letter match. Otherwise might get false positive
65+
//only proceed if the first letter match. Otherwise might get false positive
6566
if(diagonal1){
6667
if(target == board[2].charAt(2)){
6768
return true;
6869
}
6970
}
70-
71+
7172
boolean diagonal2 = target == board[0].charAt(2);
7273
if(diagonal2){
7374
if(target == board[2].charAt(0)){
@@ -76,4 +77,4 @@ private boolean hasWon(char target, String[] board){
7677
}
7778
return false;
7879
}
79-
}
80+
}

0 commit comments

Comments
 (0)