Amazon |Tech Screen| SF SDE

PYTHON

You’re given a matrix where each element in the matrix is either a “W”, “L”, or “S.” “W” represents Water, “L” represents Land, and “S” represents Statue. You can assume that the area surrounding the matrix is all Water.

An island is defined as “L” or “S” elements in the matrix that are connected horizontally or vertically. Write a function that returns the number of islands in the matrix that have Statues.

Example 0: [
[W, W, W, L, W],
[W, W, L, L, L],
[L, L, W, W, W],
[W, W, W, W, W],
[W, W, W, L, W]
]

Example 1: [
[W, W, W, L, W],
[W, W, S, L, L],
[S, L, W, W, W],
[W, W, W, W, W],
[W, W, W, S, W]
]

There are 3 islands with Statues here.

Example 2: [
[W, W, W, L, W],
[W, W, L, L, L],
[L, L, W, L, W],
[L, W, W, W, L],
[W, W, W, S, S]
]

There is 1 island with a Statue here.

Comments (2)