Count Connect-Four Scoring Lines
Problem statement
You are given a non-empty rectangular board grid. A dot (.) is an empty cell, and each uppercase English letter is one piece type.
A scoring line is a contiguous window of exactly four cells containing the same nonempty letter in one of four orientations: horizontal, vertical, diagonal down-right, or diagonal down-left. Count every distinct four-cell window once. Overlapping windows count separately, so a straight run of five equal letters contributes two scoring lines.
Return the total number of scoring lines. Treat the grid as a trusted board snapshot; you do not need to validate gravity or move history.
Function
countScoringLines(grid: String[]) → intExamples
Example 1
grid = [".......",".......",".......",".......","AAAA...","BBBB..."]return = 2The bottom two rows each contain one horizontal four-letter window.
Example 2
grid = ["AAAAA"]return = 2The length-five run contains two overlapping length-four windows, starting at columns 0 and 1.
Example 3
grid = ["X..Y..Z","X.Y...Z","XY....Z","Y.....Z"]return = 2The board has one down-left diagonal of Y and one vertical line of Z.
Constraints
1 <= grid.length <= 1000.1 <= grid[r].length <= 1000.- Every row has the same length, and the board has at most
200000cells. - Every cell is
.or an uppercase English letter.