Medium

Quiz

#419 Battleships in a Board

APPROACH

An m x n character grid board uses 'X' to mark battleship cells and '.' to mark water. Every battleship occupies a contiguous horizontal or vertical sequence of 'X' cells. No two battleships are adjacent — at least one '.' separates any two battleships in all four directions.

Return the total number of distinct battleships on the board.

Example 1:

Input: board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
Output: 2

Example 2:

Input: board = [["."]]
Output: 0
1 of 4
1:00

What is the optimal approach for this problem?