#419

Battleships in a Board

pupil · 505 · lc medium +27 · verified · 77.4% accepted · 2,509 likes · top 89%

play →

Description

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

Code

1
2
3