C++ || 0ms Solution with 10 Testcases Examples ||
class Solution {\npublic:\n    bool validTicTacToe(vector<string>& board) {\n      \n        int countO = 0 , countX = 0 ;\n        for(int i = 0 ; i<3 ; i++)\n
```class Solution {\npublic:\n    bool validTicTacToe(vector<string>& board) {\n      \n        int countO = 0 , countX = 0 ;\n        for(int i = 0 ; i<3 ; i++)\n        {\n            for(auto c : board[i])\n            {\n                if(c == \'X\')\n                    countX++;\n                else if(c == \'O\')\n                    countO++;\n            }\n        }\n        \n      Observations - \n    1. Number of \'O\' > Number of X -> return false\n    2. Number of X - Number of Os > 1 -> Return false\n    3. If O wins -\n        a) Number of Os  == Number of Xs\n        b) And X should not be winning\n    4. If X wins -\n        a) O should not win\n        if(countX != countO  && countX - countO != 1 )\n            return false;\n        \n        bool checkX = false ,  checkO = false;\n        \n        \n        // Checking in Horizontal Line having saming digit \n        if(board[0] == "XXX" ||  board[1] == "XXX" ||  board[2] == "XXX"  )\n            checkX = true;\n    \n        if(board[0] == "OOO" ||  board[1] == "OOO" ||  board[2] == "OOO")\n           checkO = true;\n        \n        if(checkX  || checkO)\n        {\n            if(countX - countO == 1)\n            {\n                 if(checkX)\n                   return !checkO;\n                return false;\n            } \n            else\n            {\n                if(checkX)\n                    return false;\n                return true;\n            }\n        }\n        \n        \n        string s1 , s2 , s3 ;\n        \n        s1.push_back(board[0][0]);\n        s1.push_back(board[1][0]);\n        s1.push_back(board[2][0]);\n        \n        s2.push_back(board[0][1]);\n        s2.push_back(board[1][1]);\n        s2.push_back(board[2][1]);\n        \n        s3.push_back(board[0][2]);\n        s3.push_back(board[1][2]);\n        s3.push_back(board[2][2]);\n        \n        // Checking in Vertical Line  having saming digit \n        if(s1 == "XXX" ||  s2 == "XXX" ||  s3 == "XXX"  )\n            checkX = true;\n        \n        if(s1 == "OOO" ||  s2 == "OOO" ||  s3 == "OOO")\n           checkO = true;\n        \n        \n        \n        if(checkX  || checkO)\n        {\n            if(countX - countO == 1)\n            {\n                 if(checkX)\n                   return !checkO;\n                 return false;\n            } \n            else\n               if(checkX)\n                    return false;\n                return true;\n        }\n        \n        \n        string diag1 , diag2;\n        \n        diag1.push_back(board[0][0]);\n        diag1.push_back(board[1][1]);\n        diag1.push_back(board[2][2]);\n        \n        diag2.push_back(board[0][2]);\n        diag2.push_back(board[1][1]);\n        diag2.push_back(board[2][0]);\n        \n        \n        // Checking in Diagonal  having saming digit \n        if(diag1 == "XXX" ||  diag2 == "XXX"  )\n            checkX = true;\n        \n        if(diag1 == "OOO" ||  diag2 == "OOO"  )\n            checkO = true;\n        \n        if(checkX || checkO)\n        {\n            if(countX - countO == 1)\n                return checkX;\n            else\n                return checkO;\n        }\n        \n        return true;\n        \n        \n    }\n};\n\n// ["XO ","XO ","XO "]\n// ["XOX"," X ","   "]\n// ["XOX","O O","XOX"]\n// ["XOX","OOO","XOX"]\n// ["XOX","OOO","XXX"]\n// ["XOX","XXO","OXO"]\n// ["XXX","OOO","   "]\n// ["XOX","OOO","XXX"]\n// ["XOX","XOO","XOX"]\n// ["OXX","XOX","OXO"]\n\n\n```\n\n![image](https://assets.leetcode.com/users/images/5d4aa5b4-b619-4a1c-a71f-c44699ad45f6_1664030843.5149136.png)\n
['C']