All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2301_2400.s2352_equal_row_and_column_pairs.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.32
Show newest version
package g2301_2400.s2352_equal_row_and_column_pairs

// #Medium #Array #Hash_Table #Matrix #Simulation
// #2023_07_02_Time_339_ms_(100.00%)_Space_48.6_MB_(96.25%)

class Solution {
    fun equalPairs(grid: Array): Int {
        val rows: MutableMap = HashMap()
        for (i in grid.indices) {
            val hash = getRowHash(grid[i])
            rows[hash] = rows.getOrDefault(hash, 0) + 1
        }
        var count = 0
        for (i in grid.indices) {
            val hash = getColHash(grid, i)
            count += rows.getOrDefault(hash, 0)
        }
        return count
    }

    private fun getRowHash(grid: IntArray): Int {
        var res = 11
        for (i in grid) res = res * 11 + i
        return res
    }

    private fun getColHash(grid: Array, index: Int): Int {
        var res = 11
        for (i in grid.indices) {
            res = res * 11 + grid[i][index]
        }
        return res
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy