Ruby: Get statistic, filter if win, recursion for rest.
Leetcode: 794. Valid Tic-Tac-Toe State.\n\nRuby: Get statistic, filter if win, recursion for rest.\n\nFirst acquire statistic number of symbols and check wins. 
## Leetcode: 794. Valid Tic-Tac-Toe State.\n\n**Ruby: Get statistic, filter if win, recursion for rest.**\n\nFirst acquire statistic number of symbols and check wins. If both types wins it is an error. If `X` wins number of `X` is greater by 1 then number of `O`. If `O` wins number of `X` and `O` are equal. After finishing working with wins, run recursive functions that pics `X` and `O` in turns, and check if finish is correct. When there is no one type of symbols to select, there must no be unselected symbols.   \n\nConvert grid to a binary representation an integer, where selected bits are marks.   \n\n\n![Converting grid to bites.](https://assets.leetcode.com/users/images/5a655b33-5536-4637-bfed-5bf96fac8c11_1617295222.8295074.png)\n\n\n\nRuby code:\n```Ruby\n# Leetcode: 794. Valid Tic-Tac-Toe State.\n# https://leetcode.com/problems/valid-tic-tac-toe-state/\n# Runtime: 52 ms, faster than 100.00% of Ruby online submissions for Valid Tic-Tac-Toe State.\n# Memory Usage: 210.1 MB, less than 100.00% of Ruby online submissions for Valid Tic-Tac-Toe State.\n# Thanks God!\n# @param {String[]} board\n# @return {Boolean}\ndef valid_tic_tac_toe(board)\n    @b = board.map(&:chars).flatten\n    @xw,@yw = false, false # x win, y win;\n    @xn, @yn = 0,0\n    @marks_mask = 0\n    get_stat()\n    return false if @xw && @yw\n    if @xw\n        return @xn == @yn + 1\n    end\n    if @yw\n        return @yn == @xn\n    end\n    return check(true,0, 0, 0)\nend\n\n# xm - x_marks;\n# ym - y_marks;\n# am - all_marks;\n\ndef check(x_turn, xm, ym, am)\n    char = x_turn ? ?X : ?O\n    # p "#{char} #{am.to_s(2)}"\n    nt = ! x_turn # next turn;\n    cm = x_turn ? xm : ym # current mark;\n     (0...9).reverse_each do |i|\n        bit = 1 << (8 - i)\n        # Need to select only unmarked!\n        if @b[i] == char && (0 == bit & am)\n            nm = cm | bit # next mark;\n            ng = am | bit # next global;\n            return (x_turn ? check(nt,nm,ym,ng) : check(nt,xm,nm,ng))\n        end\n    end\n    check_no_unmarked(am)\nend\n\ndef check_win(marks)\n    [7, 56, 448, 292, 146, 73, 273, 84].any?{|x| 0 == ((marks & x) ^ x)}\nend\n\ndef get_stat()\n    xm, ym = 0,0\n    (0...9).reverse_each do |i|\n        if ?X == @b[i] || ?O == @b[i]\n            bit = (1 << (8-i))\n            @marks_mask = @marks_mask | bit\n            if ?X == @b[i]\n                xm = xm | bit \n                @xn += 1 \n                @xw = true if check_win(xm)\n            end\n            if ?O == @b[i]\n                ym = ym | bit\n                @yn += 1\n                @yw = true  if check_win(ym)\n            end\n        end\n    end\nend\ndef check_no_unmarked(mask)\n    0 == (mask ^ @marks_mask)\nend\n\n```
['Recursion', 'Ruby']