419. Battleships in a Board
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to count the number of battleships on a 2D board represented by a matrix of 'X's (battleship parts) and '.'s (empty water). The key properties of battleships are:
Solution
Battleships in a Board in Python
class Solution:
def countBattleships(self, board: List[List[str]]) -> int:
m = len(board)
if m == 0:
return 0
n = len(board[0])
count = 0
for i in range(m):
for j in range(n):
# If the current cell is not a battleship part, skip.
if board[i][j] == '.':
continue
# Check if this 'X' is the top-left 'head' of a battleship.
# A battleship head does not have an 'X' above it or to its left.
# Check for 'X' above. If i=0, there's nothing above.
if i > 0 and board[i - 1][j] == 'X':
continue # This is part of a vertical ship, already counted by its head.
# Check for 'X' to the left. If j=0, there's nothing to the left.
if j > 0 and board[i][j - 1] == 'X':
continue # This is part of a horizontal ship, already counted by its head.
# If we reach here, this 'X' is the start of a new battleship.
count += 1
return count
Battleships in a Board in Java
class Solution {
public int countBattleships(char[][] board) {
int m = board.length;
if (m == 0) {
return 0;
}
int n = board[0].length;
int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// If the current cell is not a battleship part, skip.
if (board[i][j] == '.') {
continue;
}
// Check if this 'X' is the top-left 'head' of a battleship.
// A battleship head does not have an 'X' above it or to its left.
// Check for 'X' above. If i=0, there's nothing above.
if (i > 0 && board[i - 1][j] == 'X') {
continue; // This is part of a vertical ship, already counted by its head.
}
// Check for 'X' to the left. If j=0, there's nothing to the left.
if (j > 0 && board[i][j - 1] == 'X') {
continue; // This is part of a horizontal ship, already counted by its head.
}
// If we reach here, this 'X' is the start of a new battleship.
count++;
}
}
return count;
}
}
Battleships in a Board in C++
class Solution {
public:
int countBattleships(vector<vector<char>>& board) {
int m = board.size();
if (m == 0) return 0;
int n = board[0].size();
int count = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
// If the current cell is not a battleship part, skip.
if (board[i][j] == '.') {
continue;
}
// Check if this 'X' is the top-left 'head' of a battleship.
// A battleship head does not have an 'X' above it or to its left.
// Check for 'X' above. If i=0, there's nothing above.
if (i > 0 && board[i - 1][j] == 'X') {
continue; // This is part of a vertical ship, already counted by its head.
}
// Check for 'X' to the left. If j=0, there's nothing to the left.
if (j > 0 && board[i][j - 1] == 'X') {
continue; // This is part of a horizontal ship, already counted by its head.
}
// If we reach here, this 'X' is the start of a new battleship.
count++;
}
}
return count;
}
};
Battleships in a Board in TypeScript
function countBattleships(board: string[][]): number {
const m = board.length;
if (m === 0) {
return 0;
}
const n = board[0].length;
let count = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
// If the current cell is not a battleship part, skip.
if (board[i][j] === '.') {
continue;
}
// Check if this 'X' is the top-left 'head' of a battleship.
// A battleship head does not have an 'X' above it or to its left.
// Check for 'X' above. If i=0, there's nothing above.
if (i > 0 && board[i - 1][j] === 'X') {
continue; // This is part of a vertical ship, already counted by its head.
}
// Check for 'X' to the left. If j=0, there's nothing to the left.
if (j > 0 && board[i][j - 1] === 'X') {
continue; // This is part of a horizontal ship, already counted by its head.
}
// If we reach here, this 'X' is the start of a new battleship.
count++;
}
}
return count;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.