Trains the technique from
LeetCode 419Battleships in a BoardThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A chart is given as board, each cell holding '.' for open water or 'X' for part of a vessel.
Every vessel lies in a single straight line, either along one row or down one column, and occupies a run of neighbouring cells. No two vessels touch, so between any two of them there is at least one cell of open water.
Return how many vessels are on the chart.
Example 1
There is a single cell at the top left, a run of three down the last column, and a run of two along the bottom row, which is three vessels.
Example 2
The three cells form one vessel lying along the row, counted once at its left-hand end.
Example 3
The chart is all open water.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_battleships(board: list[list[str]]) -> int:public int countBattleships(char[][] board)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.