Skip to the code
cf.
+10 XP

#422 Outline Length of a Selected Shape

easy

A vector tool draws a stroke around the shape you click. The canvas is a grid of strings where "#" is filled and "." is empty. Filled cells that touch up, down, left, or right form one shape.

Write outlineLength(canvas, row, col) that returns the length of the outline of the shape containing cell (row, col): the number of cell sides of that shape that touch an empty cell or the edge of the canvas. If (row, col) is empty, return 0.

Example 1

Input: canvas = ["##.","#..","..#"], row = 0, col = 0
Output: 8

The L-shape has 3 cells with 12 sides, and 2 pairs of sides are shared inside the shape, leaving 12 - 4 = 8.

Example 2

Input: canvas = ["##.","#..","..#"], row = 2, col = 2
Output: 4

The single cell in the corner has 4 outside sides.

Constraints

  • 1 <= canvas.length, canvas[i].length <= 100
  • (row, col) is inside the canvas.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).