Black Shapes

Given N * M field of O's and X's, where O=white, X=black
Return the number of black shapes. A black shape consists of one or more adjacent X's (diagonals not included)

Example:

OOOXOOO
OOXXOXO
OXOOOXO

answer is 3 shapes are  :
(i)    X
     X X
(ii)
      X
 (iii)
      X
      X

Note that we are looking for connected shapes here.

For example,

XXX
XXX
XXX

is just one single connected black shape.

Interview Code Editor
Hints
  • Solution Approach
  • Complete Solution
2969 successful submissions.
Asked In:
  • Amazon
Click here to jump start your coding interview preparation