equal-row-and-column-pairs.dart (1022B)
1 //Given a m * n grid return the number of pairs you can make where 2 //a pair is a row == column. 3 //To solve I created a string for each column and compared each row with the columns 4 //returning the number of times that row's values appeared in the columns list. 5 6 //Time: 552ms Beats: 100% 7 //Memory: 180.8MB Beats: 100% 8 class Solution { 9 int equalPairs(List<List<int>> grid) { 10 int return_val = 0; 11 Map<String, int> rows = {}; 12 for(int i = 0 ; i < grid.length ; ++i){ 13 String current = ""; 14 for(int x = 0 ; x < grid[i].length ; ++x){ 15 current += grid[i][x].toString() + ' '; 16 } 17 rows[current] = (rows[current] ?? 0) + 1; 18 } 19 for(int x = 0 ; x < grid[0].length ; ++x){ 20 String curr = ""; 21 for(int i = 0 ; i < grid[0].length ; ++i){ 22 curr += grid[i][x].toString() + " "; 23 } 24 if(rows.containsKey(curr)){ 25 return_val += (rows[curr] ?? 0); 26 } 27 } 28 return return_val; 29 } 30 }