-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchingRowNCol_2.java
41 lines (34 loc) · 1.43 KB
/
MatchingRowNCol_2.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class MatchingRowNCol_2 {
public static void main(String[] args) {
System.out.println("MachingGrid_2: E 1 "+matchingRowNCol_2(new int[][] {{3,2,1},
{2,7,6},
{1,7,7}}));
System.out.println("MachingGrid_2: E 3 "+matchingRowNCol_2(new int[][] {{3,1,2,2},{1,4,4,5},{2,4,2,2},{2,4,2,2}}));
System.out.println("MachingGrid_2: E 0 "+matchingRowNCol_2(new int[][] {{3,1,},{1,4,4,5},{2,4,2,2},{2,4,2,2}}));
System.out.println("MachingGrid_2: E 2 "+matchingRowNCol_2(new int[][] {{11,1},{1,11}}));
}
public static int matchingRowNCol_2(int[][] grid){
int counter = 0;
int rows = grid.length;
int cols = grid[0].length;
if(rows != cols) return 0;
for (int row = 0; row < grid.length ; row++) {
for (int col = 0; col < grid.length ; col++) {
counter+=1;
int ele = grid[row][col];
for (int iR = 0; iR < rows ; iR++) {
if(grid[row][iR]!=grid[iR][col]){
counter-=1;
break;
}
}
}
}
return counter;
}
}