From f11517999131d8d291a7dd9ff8b5b3ee77eb020c Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 14 Jun 2023 23:43:34 +0200 Subject: [PATCH] =?UTF-8?q?problems(java):=20add=20=E2=80=9C2352.=20Equal?= =?UTF-8?q?=20Row=20and=20Column=20Pairs=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- problems/java/equal-row-and-column-pairs.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 problems/java/equal-row-and-column-pairs.java diff --git a/problems/java/equal-row-and-column-pairs.java b/problems/java/equal-row-and-column-pairs.java new file mode 100644 index 0000000..08571ee --- /dev/null +++ b/problems/java/equal-row-and-column-pairs.java @@ -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; + } +}