LC 0419 [M] Battleships in a Board - ALawliet/algorithms GitHub Wiki

class Solution:
    def countBattleships(self, board: List[List[str]]) -> int:
        n_rows = len(board)
        n_cols = len(board[0])
        count = 0
        for r in range(n_rows):
            for c in range(n_cols):
                if board[r][c] != 'X': continue
                # if current cell is X, then there can be no adjacent 
                if r > 0 and board[r-1][c] == 'X': continue
                if c > 0 and board[r][c-1] == 'X': continue
                count += 1
        return count