# Leetcode 810 Solution

https://leetcode.com/problems/valid-tic-tac-toe-state

## Solution

``````class Solution:
def getWins(self, ch, board):
wins = 0

for i in range(3):
if board[i] == ch and board[i] == ch and board[i] == ch:
wins += 1

for j in range(3):
if board[j] == ch and board[j] == ch and board[j] == ch:
wins += 1

if board == ch and board == ch and board == ch:
wins += 1

if board == ch and board == ch and board == ch:
wins += 1

return wins

def isValid(self, ch, board):
valid = False

for i in range(3):
for j in range(3):
old_str = board[i]
board[i] = board[i][:j - 1] + ' ' + board[i][j:]

if self.getWins(ch, board) == 0:
valid = True

board[i] = old_str

return valid

def validTicTacToe(self, board: List[str]) -> bool:
cnt1 = 0
cnt2 = 0

for i in range(3):
for j in range(3):
if board[i][j] == 'X':
cnt1 += 1
elif board[i][j] == 'O':
cnt2 += 1

wins1 = self.getWins('X', board)
wins2 = self.getWins('O', board)

if wins1 > 1:
return self.isValid('X', board) and cnt1 == cnt2 + 1 and wins2 == 0
elif wins1 == 1:
return wins2 == 0 and cnt1 == cnt2 + 1
else:
if wins2 == 0 and (cnt1 == cnt2 or cnt1 == cnt2 + 1):
return True
elif wins2 == 1 and cnt1 == cnt2:
return True
else:
return self.isValid('O', board) and cnt1 == cnt2
``````