-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualRowAndColumnPairs.java
More file actions
260 lines (192 loc) · 6.96 KB
/
EqualRowAndColumnPairs.java
File metadata and controls
260 lines (192 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package Algorithms.Hashing;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 15 April 2025
*/
public class EqualRowAndColumnPairs {
public static void main(String[] args) {
// int[][] grid = {{18,18,18,18,18},
// {18,18,18,18,17},
// {18,18,18,18,18},
// {18,18,18,18,18},
// {18,18,18,18,18}};
int[][] grid = {{11,1},
{1,11}};
System.out.println("equalPairsMyApproach(grid) => " + equalPairsMyApproach(grid));
}
public static int equalPairsMyApproach(int[][] grid) {
int n = grid.length;
StringBuilder[] rows = new StringBuilder[n], cols = new StringBuilder[n];
Map<String, Integer> rowMap = new HashMap<>(), colMap = new HashMap<>();
for(int i=0; i<n; i++) {
rows[i] = new StringBuilder();
cols[i] = new StringBuilder();
}
// rows
for(int r=0; r<n; r++) {
for(int c=0; c<n; c++) {
rows[r].append(grid[r][c]).append(",");
}
rowMap.merge(rows[r].toString(), 1, Integer::sum);
}
// cols
for(int c=0; c<n; c++) {
for(int r=0; r<n; r++) {
cols[c].append(grid[r][c]).append(",");
}
colMap.merge(cols[c].toString(), 1, Integer::sum);
}
int count = 0;
for(String sb1: rowMap.keySet()) {
if(colMap.containsKey(sb1)) {
count += rowMap.get(sb1) * colMap.get(sb1);
}
}
return count;
}
public static int equalPairs(int[][] grid) {
int n = grid.length;
Map<String, Integer> rowMap = new HashMap<>();
// rows
for (int r = 0; r < n; r++) {
StringBuilder sb = new StringBuilder();
for (int c = 0; c < n; c++) {
sb.append(grid[r][c]).append(",");
}
String rowStr = sb.toString();
rowMap.merge(rowStr, 1, Integer::sum);
}
// cols
int count = 0;
for (int c = 0; c < n; c++) {
StringBuilder sb = new StringBuilder();
for (int r = 0; r < n; r++) {
sb.append(grid[r][c]).append(",");
}
String colStr = sb.toString();
count += rowMap.getOrDefault(colStr, 0);
}
return count;
}
public static int equalPairs2(int[][] grid) {
int n = grid.length;
int count = 0;
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
boolean isSame = true;
for (int i = 0; i < n; i++) {
if (grid[r][i] != grid[i][c]) {
isSame = false;
break;
}
}
if (isSame) count++;
}
}
return count;
}
/**
* Even though {@link #equalPairsMyApproach2()} looks cleaner with fewer loops than {@link #equalPairsMyApproach()}
* it does: Inefficient memory access (grid[c][r])
* More work per iteration (building both row and col together)
* Higher GC impact from object allocation in loop
* And that’s why equalPairs() ends up being faster in practice
*/
public static int equalPairsMyApproach2(int[][] grid) {
int n = grid.length;
StringBuilder[] rows = new StringBuilder[n], cols = new StringBuilder[n];
Map<String, Integer> rowMap = new HashMap<>(), colMap = new HashMap<>();
// rows & cols
for(int r=0; r<n; r++) {
rows[r] = new StringBuilder();
cols[r] = new StringBuilder();
for(int c=0; c<n; c++) {
rows[r].append(rows[r].length() == 0 ? "" : " ").append(grid[r][c]);
cols[r].append(cols[r].length() == 0 ? "" : " ").append(grid[c][r]);
}
rowMap.merge(rows[r].toString(), 1, Integer::sum);
colMap.merge(cols[r].toString(), 1, Integer::sum);
}
int count = 0;
for(String sb1: rowMap.keySet()) {
if(colMap.containsKey(sb1)) {
count += rowMap.get(sb1) * colMap.get(sb1);
}
}
return count;
}
public static int equalPairsMyApproachSlow(int[][] grid) {
int n = grid.length;
StringBuilder[] rows = new StringBuilder[n], cols = new StringBuilder[n];
for(int i=0; i<n; i++) {
rows[i] = new StringBuilder();
cols[i] = new StringBuilder();
}
// rows
for(int r=0; r<n; r++) {
for(int c=0; c<n; c++) {
rows[r].append(rows[r].length() == 0 ? "" : " ").append(grid[r][c]);
}
}
// cols
for(int c=0; c<n; c++) {
for(int r=0; r<n; r++) {
cols[c].append(cols[c].length() == 0 ? "" : " ").append(grid[r][c]);
}
}
int count = 0;
for(StringBuilder sb1: rows) {
for(StringBuilder sb2: cols) {
if (sb1.toString().equals(sb2.toString())) count++;
}
}
return count;
}
@SuppressWarnings("unchecked")
public static int equalPairsMyApproachOldNotWorking(int[][] grid) {
int n = grid.length;
Map<Integer, Integer>[] rows = new Map[n], cols = new Map[n];
for(int i=0; i<n; i++) {
rows[i] = new LinkedHashMap<>();
cols[i] = new LinkedHashMap<>();
}
// rows
for(int r=0; r<n; r++) {
for(int c=0; c<n; c++) {
rows[r].merge(grid[r][c], 1, Integer::sum);
}
}
// cols
for(int c=0; c<n; c++) {
for(int r=0; r<n; r++) {
cols[c].merge(grid[r][c], 1, Integer::sum);
}
}
int count = 0;
for(Map<Integer, Integer> map1: rows) {
for(Map<Integer, Integer> map2: cols) {
if (map1.size() != map2.size()) continue;
Iterator<Map.Entry<Integer, Integer>> it1 = map1.entrySet().iterator();
Iterator<Map.Entry<Integer, Integer>> it2 = map2.entrySet().iterator();
boolean isSame = true;
while (it1.hasNext() && it2.hasNext()) {
Map.Entry<Integer, Integer> e1 = it1.next();
Map.Entry<Integer, Integer> e2 = it2.next();
if (!e1.getKey().equals(e2.getKey()) || !e1.getValue().equals(e2.getValue())) { // or Objects.equals(e1.getValue(), e2.getValue())
isSame = false;
break;
}
}
if(isSame) {
System.out.println(map1);
count++;
}
}
}
return count;
}
}