Battleships in a Board LeetCode Solution

Last updated on July 19th, 2024 at 10:23 pm

Here, We see Battleships in a Board LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Array

Companies

Microsoft

Level of Question

Medium

Battleships in a Board LeetCode Solution

Battleships in a Board LeetCode Solution

Problem Statement

Given an m x n matrix board where each cell is a battleship ‘X’ or empty ‘.’, return the number of the battleships on board.

Battleships can only be placed horizontally or vertically on board. In other words, they can only be made of the shape 1 x k (1 row, k columns) or k x 1 (k rows, 1 column), where k can be of any size. At least one horizontal or vertical cell separates between two battleships (i.e., there are no adjacent battleships).

Example 1:

battelship grid

Input: board = [[“X”,”.”,”.”,”X”],[“.”,”.”,”.”,”X”],[“.”,”.”,”.”,”X”]]
Output: 2

Example 2:
Input: board = [[“.”]]
Output: 0

1. Battleships in a Board Leetcode Solution C++

struct Solution {
    int countBattleships(vector<vector<char>>& board) {
        int ans = 0;
        for (int i = 0; i < board.size(); i++)
            for (int j = 0; j < board[0].size(); j++)
                if ('X' == board[i][j] && (!i || '.' == board[i - 1][j]) && (!j || '.' == board[i][j - 1]))
                    ans++;
        return ans;
    }
};

2. Battleships in a Board Leetcode Solution Java

class Solution {
    public int countBattleships(char[][] board) {
        if (board == null) {
            throw new IllegalArgumentException("Input is null");
        }
        if (board.length == 0 || board[0].length == 0) {
            return 0;
        }
        int rows = board.length;
        int cols = board[0].length;
        int count = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (board[i][j] == 'X'
						&& (j == cols - 1 || board[i][j + 1] == '.')
                        && (i == rows - 1 || board[i + 1][j] == '.')) {
                    count++;
                }
            }
        }
        return count;
    }
}

3. Battleships in a Board Solution JavaScript

var countBattleships = function(board) {
    let count = 0;
    for (let i = 0; i < board.length; i++) {
        for (let j = 0; j < board[i].length; j++) {
            if (board[i][j] === 'X' && board[i][j-1] !== 'X' && (!board[i-1] || board[i-1][j] !== 'X')) count++;
        }
    }
    return count;
};

4. Battleships in a Board Solution Python

class Solution(object):
    def countBattleships(self, board):
        count = 0 
        for i, row in enumerate(board):
            for j, cell in enumerate(row):
                if cell == "X":
                    if (i == 0 or board[i - 1][j] == ".") and\
                       (j == 0 or board[i][j - 1] == "."):
                            count += 1
        return count
Scroll to Top