Tic-tac-toe is played by two players A and B on a 3 x 3 grid. The rules of Tic-Tac-Toe are:


	Players take turns placing characters into empty squares ' '.
	The first player A always places 'X' characters, while the second player B always places 'O' characters.
	'X' and 'O' characters are always placed into empty squares, never on filled ones.
	The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.
	The game also ends if all squares are non-empty.
	No more moves can be played if the game is over.


Given a 2D integer array moves where moves[i] = [rowi, coli] indicates that the ith move will be played on grid[rowi][coli]. return the winner of the game if it exists (A or B). In case the game ends in a draw return "Draw". If there are still movements to play return "Pending".

You can assume that moves is valid (i.e., it follows the rules of Tic-Tac-Toe), the grid is initially empty, and A will play first.

&nbsp;
Example 1:

Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
Output: "A"
Explanation: A wins, they always play first.


Example 2:

Input: moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
Output: "B"
Explanation: B wins.


Example 3:

Input: moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
Output: "Draw"
Explanation: The game ends in a draw since there are no moves to make.


&nbsp;
Constraints:


	1 &lt;= moves.length &lt;= 9
	moves[i].length == 2
	0 &lt;= rowi, coli &lt;= 2
	There are no repeated elements on moves.
	moves follow the rules of tic tac toe.


class Solution:
    def tictactoe(self, moves: List[List[int]]) -> str:
        wins = [
            [(0, 0), (0, 1), (0, 2)],
            [(1, 0), (1, 1), (1, 2)],
            [(2, 0), (2, 1), (2, 2)],
            [(0, 0), (1, 0), (2, 0)],
            [(0, 1), (1, 1), (2, 1)],
            [(0, 2), (1, 2), (2, 2)],
            [(0, 0), (1, 1), (2, 2)],
            [(0, 2), (1, 1), (2, 0)],
        ]
    
        def checkWin(S):
            for win in wins:
                flag = True
                for pos in win:
                    if pos not in S:
                        flag = False
                        break
                if flag:
                    return True
            return False
        
        A, B = set(), set()
        for i, (x, y) in enumerate(moves):
            if i % 2 == 0:
                A.add((x, y))
            else:
                B.add((x, y))
        
        if checkWin(A):
            return 'A'
        elif checkWin(B):
            return 'B'
        
        return "Draw" if len(moves) == 9 else "Pending"
/**
Here is my solution : 

Time Complexity O(M) 
Space Complaexity O(1)
*/

class Solution {
    public String tictactoe(int[][] moves) {
        
        int [][] rcd = new int[3][3]; // rcd[0] --> rows , rcd[1] --> columns , rcd[2] --> diagonals
          
        for(int turn =0 ; turn < moves.length ; turn++){
            
			int AorB =-1;
            if(turn%2==0){AorB=1;}
            
            rcd[0][moves[turn][0]]+= AorB; 
            rcd[1][moves[turn][1]]+= AorB; 
            
            if(moves[turn][0]== moves[turn][1]){rcd[2][0]+=AorB;}     // first diagonal
            if(moves[turn][0]+moves[turn][1]-2 == 0){rcd[2][1]+=AorB;} //2nd diagonal                
            
            if( Math.abs(rcd[0][moves[turn][0]]) == 3 || Math.abs(rcd[1][moves[turn][1]]) == 3 
               ||Math.abs(rcd[2][0]) ==3 || Math.abs(rcd[2][1]) ==3  ){
             
			 return AorB == 1 ? "A" : "B"; }
                                                         } 
        
        return moves.length == 9 ? "Draw" : "Pending";
        
    }
}
class Solution {
public:
    string tictactoe(vector<vector<int>>& moves)
    {
        vector<vector<char>> grid(3,vector<char>(3));
        char val='x';
        for(auto &p:moves)
        {
            grid[p[0]][p[1]]=val;

            val=val=='x'?'o':'x';
        }
        for (int i = 0; i < 3; i++){
            //check row
            if (grid[i][0] == 'x' && grid[i][1] == 'x' && grid[i][2] == 'x')return "A";
            if (grid[i][0] == 'o' && grid[i][1] == 'o' && grid[i][2] == 'o')return "B";

            //check columns
            if (grid[0][i] == 'x' && grid[1][i] == 'x' && grid[2][i] == 'x')return "A";
            if (grid[0][i] == 'o' && grid[1][i] == 'o' && grid[2][i] == 'o')return "B";
        }
        //check diagonal
        if (grid[0][0] == 'x' && grid[1][1] == 'x' && grid[2][2] == 'x')return "A";
        if (grid[0][2] == 'x' && grid[1][1] == 'x' && grid[2][0] == 'x')return "A";
        if (grid[0][0] == 'o' && grid[1][1] == 'o' && grid[2][2] == 'o')return "B";
        if (grid[0][2] == 'o' && grid[1][1] == 'o' && grid[2][0] == 'o')return "B";

        if(moves.size()==9)
        {
            return "Draw";
        }
        return "Pending";

    }
};
//if you like the solution plz upvote.
/**
 * @param {number[][]} moves
 * @return {string}
 */
let validate = (arr) => {
    let set = [...new Set(arr)];
    return set.length == 1 && set[0] != 0;
}

var tictactoe = function(moves) {
    let grid = [[0,0,0],[0,0,0],[0,0,0]];
    for(let i in moves){
        let [x,y] = moves[i]
        grid[x][y] = (i % 2 == 1) ? -1 : 1;
        if(validate(grid[x]) 
           || validate(grid.reduce((prev, curr) => [...prev, curr[y]], []))
           || validate([grid[0][0], grid[1][1], grid[2][2]])
           || validate([grid[0][2], grid[1][1], grid[2][0]])
          )
            return (i % 2) ? "B" : "A";
    }
    return (moves.length == 9) ? "Draw" : "Pending"
};
Find Winner on a Tic Tac Toe Game