Problem statement

https://binarysearch.com/problems/Latin-Square/

Solution

Just check each row and column. Also check that in total we have n elements.

Complexity

It is O(n^2) for time and space.

Code

class Solution:
    def solve(self, M):
        n = len(M)
        s = set()
        for row in chain(M, zip(*M)):
            rset = set(row)
            if len(rset) != n: return False
            s |= rset
        return len(s) == n