Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:


You receive a valid board, made of only battleships or empty slots.
Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size.
At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.


Example:
X..X
...X
...X

In the above board there are 2 battleships.

Invalid Example:
...X
XXXX
...X

This is an invalid board that you will not receive - as battleships will always have a cell separating between them.

Follow up:Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?

Difficulty: interview

Starter code:

class Solution:
    def countBattleships(self, board: List[List[str]]) -> int:
        

Solutions:
class Solution:
     def countBattleships(self, board):
         """
         :type board: List[List[str]]
         :rtype: int
         """
         count = 0
         for i in range(len(board)):
             for j in range(len(board[i])):
                 if board[i][j] == 'X':
                     if i-1 < 0 and j-1 < 0:
                         count += 1
                     elif i-1 < 0 and board[i][j-1] != 'X':
                         count += 1
                     elif j-1 < 0 and board[i-1][j] != 'X':
                         count += 1
                     elif board[i-1][j] != 'X' and board[i][j-1] != 'X':
                         count += 1
         return count


class Solution:
     def countBattleships(self, board):
         """
         :type board: List[List[str]]
         :rtype: int
         """
         num=0
         
         record=[(-1,-1)]
         
         
         colnum=0
         for i in board:
             rownum=0
             cont=0
             for j in i:
                 
                 if j=="X":
                       if (cont==1):
                             rownum+=1
                             continue
                       else:            
                              ind=0
                              for k in record:
                                   a,b=k
                                   if (a==colnum)&(b==rownum):
                                     ind=1
                                     record.append((a+1,b))
                                     record.remove((a,b))  
                                     break
 
                              if (ind==0):
                                     num=num+1
                                     record.append((colnum+1,rownum))
                                     cont=1
                 if j!="X":
                     cont=0
                 rownum+=1           
                  
 
             colnum+=1
         
         return num
                     


class Solution:
     def countBattleships(self, board):
         """
         :type board: List[List[str]]
         :rtype: int
         """
         ans = 0
         for r, row in enumerate(board):
             for c, val in enumerate(row):
                 if val == 'X':
                     ans += 1
                     if r and board[r-1][c] == 'X':
                         ans -= 1
                     elif c and board[r][c-1] == 'X':
                         ans -= 1
         return ans