14. Largest subsquare surrounded by X
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To solve this problem, I create two additional matrices, col and row, to store the count of consecutive 'X's in the columns and rows respectively.
Then, I iterate over each cell of the matrix to find the largest subsquare surrounded by 'X' characters.
For each cell, we calculate the minimum of the counts of consecutive 'X's in its corresponding column and row. This gives us the size of the largest subsquare with the current cell as its bottom-right corner.
We then iterate over decreasing sizes of subsquares starting from this minimum size and check if all the cells in the subsquare are surrounded by 'X' characters. If they are, we update the maximum size found so far.
Finally, we return the maximum size of the subsquare found.
Time Complexity: O(n^2) where n is the size of the input matrix.
Auxiliary Space Complexity: O(n^2) for the auxiliary matrices col
and row
.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.