Tic Tac Toe | Practice | GeeksforGeeks

A Tic-Tac-Toe board is given after some moves are played. Find out if the given board is valid, i.e., is it possible to reach this board position after some moves or not.
Note that every arbitrary filled grid of 9 spaces isn’t valid e.g. a grid filled with 3 X and 6 O isn’t valid situation because each player needs to take alternate turns.
Note :  The game starts with X

Example 1:
Input:
board[] = {'X', 'X', 'O', 
           'O', 'O', 'X',
           'X', 'O', 'X'};
Output: Valid
Explanation: This is a valid board.


Example 2:
Input:
board[] = {'O', 'X', 'X', 
           'O', 'X', 'X',
           'O', 'O', 'X'};
Output: Invalid
Explanation: Both X and O cannot win.

Your Task:
You don't need to read input or print anything. Your task is to complete the function isValid() which takes a character array of size 9 representing the board as input parameter and returns a boolean value denoting if it is valid or not.

Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)

Constraints:
Every character on the board can either be 'X' or 'O'.
 
0
lindan1232 months ago
public:
    bool solve(char ar[9],char n)
    {
        if(ar[0]==ar[1] && ar[1]==ar[2] && ar[2]==n)
        {
            return true;
        }
        if(ar[0]==ar[3] && ar[3]==ar[6] && ar[6]==n)
        {
            return true;
        }
        if(ar[0]==ar[4] && ar[4]==ar[8] && ar[8]==n)
        {
            return true;
        }
        if(ar[1]==ar[4] && ar[4]==ar[7] && ar[7]==n)
        {
            return true;
        }
        if(ar[2]==ar[4] && ar[4]==ar[6] && ar[6]==n)
        {
            return true;
        }
        if(ar[2]==ar[5] && ar[5]==ar[8] && ar[8]==n)
        {
            return true;
        }
        if(ar[3]==ar[4] && ar[4]==ar[5] && ar[5]==n)
        {
            return true;
        }
        if(ar[6]==ar[7] && ar[7]==ar[8] && ar[8]==n)
        {
            return true;
        }
       return false;
    }
    bool isValid(char board[9]){
        
        int co=0;
        int cx=0;
        for(int i=0;i<9;i++)
        {
            if(board[i]=='O')
            {
                co++;
            }
            else if(board[i]=='X')
            {
                cx++;
            }
        }
        if(abs(co-cx)>=2)
        {
            return false;
        }
        else if(abs(co-cx)==1)
        {
            char o = 'O';
            char x = 'X';
            bool a = solve(board,o);
            bool b = solve(board,x);
            if(a==true && b==true)
            {
                return false;
            }
            else if(a==false && b==false)
            {
                if(co<cx)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if(a==true && b==false)
            {
                return false;
                
            }
            else if(a==false && b==true)
            {
                if(co<cx){
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
Time Taken : 0.0sec
Cpp
0
gaurav79163 months ago
    bool isValid(char board[9]){
        bool oo=false, xx=true;
        int cnto = 0, cntx = 0;
        for(int i=0; i<9; i++){
            if(board[i] == 'O')
                cnto += 1;
            else 
                cntx +=1;
        }
       int win[8][3]= {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}};
       for(int i=0; i<8; i++){
           if(board[win[i][0]]=='O' && board[win[i][1]]=='O' && board[win[i][2]]=='O')
               oo=true;
       }
       if(oo == true || cntx-cnto != 1)
           return false;
       return true;
    }
+1
noob14prosometime4 months ago
bool iswin(char x,char a[]){
        if(a[0]==a[1]&&a[1]==a[2]&&a[1]==x){
            return 1;
        }
        if(a[3]==a[4]&&a[4]==a[5]&&a[5]==x){
            return 1;
        }
        if(a[6]==a[7]&&a[7]==a[8]&&a[7]==x){
            return 1;
        }
        if(a[0]==a[3]&&a[3]==a[6]&&a[6]==x){
            return 1;
        }
        if(a[1]==a[4]&&a[4]==a[7]&&a[7]==x){
            return 1;
        }
        if(a[2]==a[5]&&a[8]==a[5]&&a[5]==x){
            return 1;
        }
        if(a[0]==a[4]&&a[4]==a[8]&&a[8]==x){
            return 1;
        }
        if(a[2]==a[4]&&a[4]==a[6]&&a[6]==x){
            return 1;
        }
        return 0;
    }
    bool isValid(char board[9]){
        // code here
        int xcount=0;
        int ocount=0;
        for(int i=0;i<9;i++){
            if(board[i]=='X'){
                xcount++;
            }
            if(board[i]=='O'){
                ocount++;
            }
        }
       if(xcount==ocount || xcount==ocount+1){
           if(iswin('O',board)){
               if(iswin('X',board)){
                   return 0;
               }
               return 0;
               
           }
           if(iswin('X',board)&&xcount!=ocount+1){
               return 0;
           }else{
               return 1;
           }
       }else{
           return 0;
       }
        
    }
0
chamoliabhishek0077 months ago
Python 0.0 soln
    def isValid(self, board):       oo=False       xx=True       xcount=0       ycount=0       for i in range(9):           if board[i]=='X':               xcount+=1           if board[i]=='O':               ycount+=1       if xcount-ycount!=1:           return False             win=[[0,1,2],               [3,4,5],               [6,7,8],               [0,3,6],               [1,4,7],               [2,5,8],               [0,4,8],               [2,4,6]]       for i in range(8):                      if board[win[i][0]]=='O' and board[win[i][1]]=='O' and board[win[i][2]]=='O':               oo=True                          if board[win[i][0]]=='X' and board[win[i][1]]=='X' and board[win[i][2]]=='X':               xx=True                  if (oo):           return False       return True
0
shadyboy7 months ago
class Solution{   public:   bool isValid(char board[9]){       bool oo = false;       bool xx = false;              int x = 0, y = 0;              for(int i = 0; i < 9 ;i++){           if(board[i]=='O')x++;           else y++;       }              if(y-x != 1)return false;              for(int i = 0; i < 9 ;i+=3){           if(board[i] =='O' && board[i+1] =='O' && board[i+2]=='O'){               oo = true;           }           if(board[i] =='X' && board[i+1] =='X' && board[i+2]=='X'){               xx = true;           }       }              for(int i = 0; i < 3 ;i++){           if(board[i] =='O' && board[i+3] =='O' && board[i+6]=='O'){               oo = true;           }           if(board[i] =='X' && board[i+3] =='X' && board[i+6]=='X'){               xx = true;           }          }              if(board[0] =='O' && board[4] =='O' && board[8]=='O'){               oo = true;           }       if(board[0] =='X' && board[4] =='X' && board[8]=='X'){           xx = true;       }               if(board[2] =='O' && board[4] =='O' && board[6]=='O'){               oo = true;           }       if(board[2] =='X' && board[4] =='X' && board[6]=='X'){           xx = true;       }               if(oo)return false;              return true;   }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.