Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Battleships in a Board

Medium

Topics
ArrayDFSMatrix

Given an m x n board where each cell is either 'X' (part of a battleship) or '.' (empty), return the number of battleships. Battleships are placed horizontally or vertically, and no two battleships are adjacent (there is at least one empty cell between them).

Example 1

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

Example 2

Input:  board = ["."]
Output: 0

Constraints

  • 1 <= m, n <= 200
  • board[i][j] is 'X' or '.'.
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access