(C++) 794. Valid Tic-Tac-Toe State
\n\nclass Solution {\npublic:\n    bool validTicTacToe(vector<string>& board) {\n        int diff = 0; \n        vector<int> freq(8); \n        unordered_map<ch
\n```\nclass Solution {\npublic:\n    bool validTicTacToe(vector<string>& board) {\n        int diff = 0; \n        vector<int> freq(8); \n        unordered_map<char, int> mp = {{\'O\', -1}, {\' \', 0}, {\'X\', 1}}; \n        for (int i = 0; i < 3; ++i) \n            for (int j = 0; j < 3; ++j) {\n                int x = mp[board[i][j]]; \n                diff += x; \n                freq[i] += x; \n                freq[j+3] += x; \n                if (i == j) freq[6] += x; \n                if (i+j == 2) freq[7] += x; \n            }\n        bool xwin = false, owin = false; \n        if (find(freq.begin(), freq.end(), 3) != freq.end()) xwin = true; \n        if (find(freq.begin(), freq.end(), -3) != freq.end()) owin = true; \n        if ((xwin && owin) || (xwin && diff != 1) || (owin && diff != 0))  return false; \n        return 0 <= diff && diff <= 1; \n    }\n};\n```
['C']