Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programming101

Learn everything about programming

Leetcode Battleships in a Board problem solution

YASH PAL, 31 July 2024

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

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

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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes