commit a9cb95245c4fc3c0732ccf6cdde7cfcaa80226c2 parent 64e93c0e587baae258cf641bc8c7f40d205cc405 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Mon, 12 Jun 2023 20:09:50 -0500 Completed equal row and column pairs problem using dart Diffstat:
| A | equal-row-and-column-pairs/equal-row-and-column-pairs.dart | | | 30 | ++++++++++++++++++++++++++++++ |
1 file changed, 30 insertions(+), 0 deletions(-)
diff --git a/equal-row-and-column-pairs/equal-row-and-column-pairs.dart b/equal-row-and-column-pairs/equal-row-and-column-pairs.dart @@ -0,0 +1,30 @@ +//Given a m * n grid return the number of pairs you can make where +//a pair is a row == column. +//To solve I created a string for each column and compared each row with the columns +//returning the number of times that row's values appeared in the columns list. + +//Time: 552ms Beats: 100% +//Memory: 180.8MB Beats: 100% +class Solution { + int equalPairs(List<List<int>> grid) { + int return_val = 0; + Map<String, int> rows = {}; + for(int i = 0 ; i < grid.length ; ++i){ + String current = ""; + for(int x = 0 ; x < grid[i].length ; ++x){ + current += grid[i][x].toString() + ' '; + } + rows[current] = (rows[current] ?? 0) + 1; + } + for(int x = 0 ; x < grid[0].length ; ++x){ + String curr = ""; + for(int i = 0 ; i < grid[0].length ; ++i){ + curr += grid[i][x].toString() + " "; + } + if(rows.containsKey(curr)){ + return_val += (rows[curr] ?? 0); + } + } + return return_val; + } +}