c++ | easy | fast
\n\n# Code\n\nclass Solution \n{\npublic:\n    bool validTicTacToe(vector<string>& grid) \n    {\n        int count_x = 0;\n        int count_o = 0;\n        \n
\n\n# Code\n```\nclass Solution \n{\npublic:\n    bool validTicTacToe(vector<string>& grid) \n    {\n        int count_x = 0;\n        int count_o = 0;\n        \n        for (int i = 0; i < 3; i++)\n        {\n            for (int j = 0; j < 3; j++)\n            {\n                if (grid[i][j] == \'X\')\n                {\n                    count_x++;\n                }\n                else if (grid[i][j] == \'O\')\n                {\n                    count_o++;\n                }\n            }\n        }\n        \n        if (count_o > count_x || count_x - count_o >= 2)\n            return false;\n        \n        if (count_x >= 3 && count_x == count_o && check_x_win(grid))\n            return false;\n        \n        if (count_o >= 3 && count_x > count_o && check_o_win(grid))\n            return false;\n        \n        return true;\n    }\nprivate:\n    bool check_x_win(const std::vector<std::string>& grid)\n    {\n        bool res = false;\n        res |= grid[0][0] == \'X\' && grid[0][1] == \'X\' && grid[0][2] == \'X\';\n        res |= grid[1][0] == \'X\' && grid[1][1] == \'X\' && grid[1][2] == \'X\';\n        res |= grid[2][0] == \'X\' && grid[2][1] == \'X\' && grid[2][2] == \'X\';\n        \n        res |= grid[0][0] == \'X\' && grid[1][0] == \'X\' && grid[2][0] == \'X\';\n        res |= grid[0][1] == \'X\' && grid[1][1] == \'X\' && grid[2][1] == \'X\';\n        res |= grid[0][2] == \'X\' && grid[1][2] == \'X\' && grid[2][2] == \'X\';\n        \n        res |= grid[0][0] == \'X\' && grid[1][1] == \'X\' && grid[2][2] == \'X\';\n        res |= grid[0][2] == \'X\' && grid[1][1] == \'X\' && grid[2][0] == \'X\';\n        \n        return res;\n    }\n    \n    bool check_o_win(const std::vector<std::string>& grid)\n    {\n        bool res = false;\n        res |= grid[0][0] == \'O\' && grid[0][1] == \'O\' && grid[0][2] == \'O\';\n        res |= grid[1][0] == \'O\' && grid[1][1] == \'O\' && grid[1][2] == \'O\';\n        res |= grid[2][0] == \'O\' && grid[2][1] == \'O\' && grid[2][2] == \'O\';\n        \n        res |= grid[0][0] == \'O\' && grid[1][0] == \'O\' && grid[2][0] == \'O\';\n        res |= grid[0][1] == \'O\' && grid[1][1] == \'O\' && grid[2][1] == \'O\';\n        res |= grid[0][2] == \'O\' && grid[1][2] == \'O\' && grid[2][2] == \'O\';\n        \n        res |= grid[0][0] == \'O\' && grid[1][1] == \'O\' && grid[2][2] == \'O\';\n        res |= grid[0][2] == \'O\' && grid[1][1] == \'O\' && grid[2][0] == \'O\';\n        \n        return res;\n    }\n};\n```
['C++']