-
Notifications
You must be signed in to change notification settings - Fork 0
/
2352. Equal Row and Column Pairs.java
58 lines (48 loc) · 1.44 KB
/
2352. Equal Row and Column Pairs.java
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
Brute Force Approach :
time complexxity :O(n^3)
space complexity :O(1)
class Solution {
public int equalPairs(int[][] grid) {
int n=grid.length;
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int count=0;
for(int k=0;k<n;k++){
if(grid[i][k]!=grid[k][j])
break;
count++;
}
System.out.println(count);
if(count==n)ans++;
}
}
return ans;
}
}
Optimisation using HashMap :
time complexity :O(n^2)
space complexity :O(n^2)
class Solution {
public int equalPairs(int[][] grid) {
//using hashmap
//time :O(n^2) space:O(n^2)
int ans = 0;
int n = grid.length;
// Keep track of the frequency of each row.
Map<String, Integer> rowVal_frq = new HashMap<>();
for (int[] row : grid) {
String rowString = Arrays.toString(row);
rowVal_frq.put(rowString, 1 + rowVal_frq.getOrDefault(rowString, 0));
}
// Add up the frequency of each column in map.
for (int c = 0; c < n; c++) {
int[] colVal = new int[n];
for (int r = 0; r < n; ++r) {
colVal[r] = grid[r][c];
}
ans += rowVal_frq.getOrDefault(Arrays.toString(colVal), 0);
}
return ans;
}
}