This question is similar to Biggest Square in a grid.
Challenge
Given a matrix of 1
and 0
in a string format "xxxx,xxxxx,xxxx,xx.."
or array format ["xxxx","xxxx","xxxx",...]
, You will create a function that determines the area of the largest square submatrix that contains all 1
.
A square submatrix is one of equal width and height, and your function should return the area of the largest submatrix that contains only 1
.
For Example:
Given "10100,10111,11111,10010"
, this looks like the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
You can see the bolded 1
create the largest square submatrix of size 2x2, so your program should return the area which is 4.
Rules
- Submatrix must be one of equal width and height
- Submatrix must contains only values
1
- Your function must return the area of the largest submatrix
- In case no submatrix is found, return
1
- You can calculate the area of the submatrix by counting the number of
1
in the submatrix
Test cases
Input: "10100,10111,11111,10010"
Output: 4
Input: "0111,1111,1111,1111"
Output: 9
Input "0111,1101,0111"
Output: 1
This is code-golf, so the shortest answer in bytes win.