package leetcode;

/**
 * https://leetcode.com/problems/valid-tic-tac-toe-state/
 */
public class Problem794 {
    public boolean validTicTacToe(String[] board) {
        // TODO
        return false;
    }

    public static void main(String[] args) {
        Problem794 prob = new Problem794();
        System.out.println(prob.validTicTacToe(new String[]{
            "O  ",
            "   ",
            "   "})); // false
        System.out.println(prob.validTicTacToe(new String[]{
            "XOX",
            " X ",
            "   "})); // false
        System.out.println(prob.validTicTacToe(new String[]{
            "XXX",
            "   ",
            "OOO"})); // false
        System.out.println(prob.validTicTacToe(new String[]{
            "XOX",
            "O O",
            "XOX"})); // true
    }
}

@@ -168,214 +168,700 @@
     
-// TODO%0A        return false;
+int oCount = 0;%0A        int xCount = 0;%0A        for (int i = 0; i %3C board.length; i++) %7B
 %0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem794 prob = new Problem794();%0A        System.out.println(prob.validTicTacToe(new String%5B%5D%7B%0A            %22O  %22,%0A
+    char%5B%5D chars = board%5Bi%5D.toCharArray();%0A            for (int j = 0; j %3C chars.length; j++) %7B%0A                if (chars%5Bj%5D == 'O') %7B%0A                    oCount++;%0A                %7D else if (chars%5Bj%5D == 'X') %7B%0A                    xCount++;%0A                %7D%0A            %7D%0A        %7D%0A        if (xCount - oCount != 1 && xCount - oCount != 0) %7B%0A            return false;%0A        %7D%0A        // Check for winning states.%0A        String leftDiag = %22%22;%0A        String rightDiag = %22%22;%0A        for (int i = 0; i %3C board.length; i++) %7B%0A            if (board%5Bi%5D.equals(%22XXX%22) %7C%7C board%5Bi%5D.equals(%22OOO%22)) %7B%0A 
     
@@ -872,45 +872,86 @@
     
-%22   %22,%0A            %22   %22%7D)); //
+   if (!isValid(board%5Bi%5D, xCount, oCount)) %7B%0A                    return
  false
+;
 %0A   
@@ -959,98 +959,271 @@
     
-System.out.println(prob.validTicTacToe(new String%5B%5D%7B%0A            %22XOX%22,%0A            %22 X %22,
+        %7D%0A            %7D%0A            String s = %22%22;%0A            for (int j = 0; j %3C board.length; j++) %7B%0A                s += board%5Bj%5D.charAt(i);%0A            %7D%0A            if (s.equals(%22XXX%22) %7C%7C s.equals(%22OOO%22)) %7B%0A                if (!isValid(s, xCount, oCount)) %7B
 %0A   
@@ -1235,106 +1235,297 @@
     
-%22
    
-%22%7D)); // false%0A        System.out.println(prob.validTicTacToe(new String%5B%5D%7B
+     return false;%0A                %7D%0A            %7D%0A            leftDiag += board%5Bi%5D.charAt(i);%0A            rightDiag += board%5Bi%5D.charAt(board.length - i - 1);
 %0A      
+  %7D%0A
       
-%22XXX%22,
+  if (leftDiag.equals(%22XXX%22) %7C%7C leftDiag.equals(%22OOO%22)) %7B%0A            if (!isValid(leftDiag, xCount, oCount)) %7B
 %0A   
@@ -1537,125 +1537,366 @@
     
-%22   %22,%0A            %22OOO%22%7D)); // false%0A        System.out.println(prob.validTicTacToe(new String%5B%5D%7B%0A            %22XOX%22,
+    return false;%0A            %7D%0A        %7D%0A        if (rightDiag.equals(%22XXX%22) %7C%7C rightDiag.equals(%22OOO%22)) %7B%0A            if (!isValid(rightDiag, xCount, oCount)) %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;%0A    %7D%0A%0A    private static boolean isValid(String winning, int xCount, int oCount) %7B%0A        if (winning.equals(%22XXX%22)) %7B
 %0A   
@@ -1908,44 +1908,234 @@
     
-%22O O%22,%0A            %22XOX%22%7D)); //
+if (xCount - oCount != 1) %7B%0A                return false;%0A            %7D%0A        %7D else if (winning.equals(%22OOO%22)) %7B%0A            if (xCount != oCount) %7B%0A                return false;%0A            %7D%0A        %7D%0A        return
  true
+;
 %0A   

Solve problem 794