Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Battleships in a Board problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Battleships in a Board problem solution, we have 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).

Leetcode Battleships in a Board problem solution

Leetcode Battleships in a Board problem solution in Python.

class Solution:
    def countBattleships(self, board: List[List[str]]) -> int:
        ships = 0
        for i, row in enumerate(board):
            for j, slot in enumerate(row):
                if slot == 'X':
                    if i >= 1 and board[i-1][j] == "X":
                        continue
                    if j >= 1 and board[i][j-1] == "X":
                        continue
                    ships += 1
        return ships

Battleships in a Board problem solution in Java.

class Solution {
    public int countBattleships(char[][] board) {
        int ans=0;
        for(int i=0;i<board.length;i++)
        {
            for(int j=0;j<board[0].length;j++)
            {
                if(board[i][j] == 'X')
                {
                    if(i == 0&& j == 0)
                      ans++;
                    else if(i==0)
                    {
                        if(board[i][j-1]!='X')
                            ans++;
                    }
                    else if(j==0)
                    {
                        if(board[i-1][j] != 'X')
                            ans++;
                    }
                    else{
                        if(board[i-1][j] != 'X' && board[i][j-1]!='X')
                            ans++;
                    }
                }
            }
        }
        return ans;
    }
}

Problem solution in C++.

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

Problem solution in C.

int countBattleships(char** board, int boardSize, int* boardColSize){
    int x,y;
    int cnt = 0;
 
    for(y = 0; y<boardSize; y++) {
        x = 0;
        while(x<boardColSize[y]) {
            while(x<boardColSize[y] && board[y][x] == '.') x++;
            // Found
            if(x == boardColSize[y])
                break;
            if (y == 0) {
                // First line. Just increase the count
                cnt ++;
                while(x<boardColSize[y] && board[y][x] == 'X') x++;
            } else {
                if(board[y-1][x] != 'X') {
                    // New ship
                    cnt++;
                }
                while(x<boardColSize[y] && board[y][x] == 'X') x++;
            }
        }
    }
    return cnt;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes