problems(java): add “2352. Equal Row and Column Pairs”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
931938ba38
commit
f115179991
1 changed files with 27 additions and 0 deletions
27
problems/java/equal-row-and-column-pairs.java
Normal file
27
problems/java/equal-row-and-column-pairs.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
class Solution {
|
||||
public int equalPairs(int[][] grid) {
|
||||
int count = 0, n = grid.length;
|
||||
|
||||
// Check each row r against each column c.
|
||||
for (int r = 0; r < n; ++r) {
|
||||
for (int c = 0; c < n; ++c) {
|
||||
boolean match = true;
|
||||
|
||||
// Iterate over row r and column c.
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (grid[r][i] != grid[i][c]) {
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If row r equals column c, increment count by 1.
|
||||
if (match) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue