Skip to content

Commit

Permalink
Added CountSquareSubMatricesWithAllOnes solution (#96)
Browse files Browse the repository at this point in the history
* Added Regular Expression matching

* Revert "Added Regular Expression matching"

This reverts commit 4449207.

* Added CountSquareSubMatricesWithAllOnes solution
  • Loading branch information
afrinc authored Oct 27, 2024
1 parent a7b0b65 commit f964c94
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class CountSquareSubMatricesWithAllOnes {
public int countSquares(int[][] matrix) {
//Find no of rows and columns
int rows=matrix.length;
int cols=matrix[0].length;

for(int i=1;i<rows;i++)
{
for(int j=1;j<cols;j++)
{
if(matrix[i][j]==1)
{
matrix[i][j]+=Math.min(matrix[i-1][j],Math.min(matrix[i][j-1],matrix[i-1][j-1]));
}
}
}

//Number of squares possible
int total=0;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
total+=matrix[i][j];
}
}
return total;
}
}

0 comments on commit f964c94

Please sign in to comment.