Distinct Row Counts
MediumPractice library · +20 XPmatrixhashingdata structures
Given an M x N binary matrix (containing only 0s and 1s), find the number of distinct rows. Two rows are considered distinct if they differ in at least one element.
Input
The first line contains two integers M and N, the dimensions of the matrix. The next M lines each contain N integers (0 or 1) representing a row of the matrix.
Output
Print a single integer: the count of distinct rows.
Constraints
1 <= M, N <= 200000. The total number of elements M * N will not exceed 200000.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
3 4 0 1 0 1 1 0 1 0 0 1 0 1
Output
2
There are 3 rows, but the first and third are identical. So, there are 2 distinct rows.
Example 2
Input
2 2 1 1 1 1
Output
1
Both rows are identical, so there is only 1 distinct row.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
3 4 0 1 0 1 1 0 1 0 0 1 0 1
Example 2 input
2 2 1 1 1 1