1
+ // Runtime: 1 ms (Top 71.13%) | Memory: 41.6 MB (Top 42.96%)
1
2
class Solution {
2
3
public boolean validTicTacToe (String [] board ) {
3
- //cnt number of X and O
4
+ //cnt number of X and O
4
5
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.
6
7
if (x >5 ){
7
8
return false ;
8
9
}
@@ -23,7 +24,7 @@ public boolean validTicTacToe(String[] board) {
23
24
}
24
25
return true ;
25
26
}
26
-
27
+
27
28
private int cntNumber (char target , String [] board ){
28
29
int res = 0 ;
29
30
for (int i = 0 ; i <3 ; i ++) {
@@ -35,7 +36,7 @@ private int cntNumber(char target, String[] board){
35
36
}
36
37
return res ;
37
38
}
38
-
39
+
39
40
private boolean hasWon (char target , String [] board ){
40
41
String toWin = Character .toString (target ).repeat (3 );
41
42
for (int i = 0 ; i <3 ; i ++) {
@@ -55,19 +56,19 @@ private boolean hasWon(char target, String[] board){
55
56
return true ;
56
57
}
57
58
}
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.
59
60
if (target != board [1 ].charAt (1 )){
60
61
return false ;
61
62
}
62
-
63
+
63
64
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
65
66
if (diagonal1 ){
66
67
if (target == board [2 ].charAt (2 )){
67
68
return true ;
68
69
}
69
70
}
70
-
71
+
71
72
boolean diagonal2 = target == board [0 ].charAt (2 );
72
73
if (diagonal2 ){
73
74
if (target == board [2 ].charAt (0 )){
@@ -76,4 +77,4 @@ private boolean hasWon(char target, String[] board){
76
77
}
77
78
return false ;
78
79
}
79
- }
80
+ }
0 commit comments