FastPrepCount Connect-Four Scoring Lines

Count Connect-Four Scoring Lines

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

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[]) → int

Examples

Example 1

grid = [".......",".......",".......",".......","AAAA...","BBBB..."]return = 2

The bottom two rows each contain one horizontal four-letter window.

Example 2

grid = ["AAAAA"]return = 2

The 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 = 2

The 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 200000 cells.
  • Every cell is . or an uppercase English letter.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int countScoringLines(String[] grid) {
    // Write your code here.
}
grid[".......",".......",".......",".......","AAAA...","BBBB..."]
expected2
Checking account…