🏁
365 Days Coding Challenge
  • Problems List
  • Week 1
    • #1 - Palindrome or not
    • #2 - Count Digits
    • #3 - Reverse the number
    • #4 - Armstrong number or not
    • #5 - Find Divisors of Number
    • #6 - Prime number or not
    • #7 - Factorial of the number
  • Week 2
    • #8 - Sum of Elements in the Array
    • #9 - Large Element in the array
    • #10 - Small Element in the array
    • #11 - Search element in the array
    • #12 - Count element in the array
    • #13 - Reverse the array
    • #14 - Filter Elements from Array
  • Week 3
    • #15 - Count Vowels and Consonants
    • #16 - Split Sentence into words
    • #17 - Remove Extra Spaces
    • #18 - Valid IP4 Address
    • #19 - String Compression
    • #20 - Anagram
    • #21 - Word Search
  • Week 4
    • #22 - Prefix Search
    • #23 - Suffix Search
    • #24 - Count Letter Frequency
    • #25 - Find Duplicate Numbers
    • #26 - Find Unique Numbers
    • #27 - Remove Duplicate Numbers
    • #28 - Move Zeros to Right End
  • Week 5
    • #29 - Sum of Elements in Matrix
    • #30 - Matrix Addition and Subtraction
    • #31 - Matrix Diagonal Sum
    • #32 - Identity Matrix
    • #33 - Convert 1D Array to 2D Array
    • #34 - Find Winner on a Tic Tac Toe Game
    • #35 - Possible moves for Horse in chess
  • Week 6
    • #36 - Check if array is sorted or not
    • #37 - Second largest element in the array (no duplicates)
    • #38 - Convert 24 hours into 12 hours format
    • #39 - Number of days between two dates
    • #40 - compare two versions
    • #41 - Sort the array
    • #42 - Median of the array
  • Week 7
    • #43 - Union of the array
    • #44 - two sum problem
    • #45 - Sort 0's, 1's and 2's array
    • #46 - Intersection of Two Array
    • #47 - Rotate Array by one position
    • #48 - Rotate Array by K times
Powered by GitBook
On this page
  • Example 1
  • Example 2
  • Example 3
  1. Week 5

#34 - Find Winner on a Tic Tac Toe Game

You are given a 3x3 Tic-Tac-Toe board as a list of lists, where each inner list represents a row in the board. Each cell in the board contains either "X", "O", or an empty space " ".

Your task is to implement a function to determine the winner of the game.

  1. The winner can be "X" if player X has filled either a row, a column, or a diagonal with their symbol.

  2. Similarly, the winner can be "O" if player O has filled either a row, a column, or a diagonal with their symbol.

  3. If there is no winner yet but board is incomplete, the game is considered to be ongoing.

  4. If there is no winner yet but board is complete, match is found to be Draw.

Example 1

Input

board =
[
    ["X", "O", "X"],
    ["O", "X", "O"],
    ["O", "X", "X"]
]

Output

Winner = "X"

Example 2

Input

board =
[
    ["X", "X", "O"],
    ["O", "O", "X"],
    ["O", "X", "X"]
]

Output

Winner = "O" 

Example 3

Input

board =
[
    ["X", " ", "O"],
    [" ", " ", "X"],
    ["O", "X", "X"]
]

Output

Ongoing Match

Example 4

Input

board =
[
    ["X", "X", "O"],
    ["O", "O", "X"],
    ["O", "X", "X"]
]

Output

Draw Match
Previous#33 - Convert 1D Array to 2D ArrayNext#35 - Possible moves for Horse in chess

Last updated 1 year ago