During an internship interview, I encountered a complex logical exercise that I couldn't solve. If you could provide some assistance and explain the solution to me, I would greatly appreciate it (even though I didn't pass the interview). The exercise is as follows:
"Given an array arr of strings, complete the function landPerimeter by calculating the total perimeter of all the islands. Land areas are marked with 'X' while water fields are represented as 'O'. Each tile is considered a 1 x 1 piece of land. Here are some examples for better visualization: ['XOOXO', 'XOOXO', 'OOOXO', 'XXOXO', 'OXOOO']
View image here :
Expected result: "Total land perimeter: 24",
while
['XOOO',
'XOXO',
'XOXO',
'OOXX',
'OOOO']
View image here:
Expected result: "Total land perimeter: 18"
I kindly request a code example demonstrating how to solve this exercise. Thank you in advance!