55. Different Squares


Task

Given a rectangular matrix containing only digits, calculate the number of different 2 × 2 squares in it.

Example

For

matrix = [[1, 2, 1],
          [2, 2, 2],
          [2, 2, 2],
          [1, 2, 3],
          [2, 2, 1]]

the output should be
differentSquares(matrix) = 6.

Here are all 6 different 2 × 2 squares:

  • 1 2
    2 2
  • 2 1
    2 2
  • 2 2
    2 2
  • 2 2
    1 2
  • 2 2
    2 3
  • 2 3
    2 1

Input/Output

  • [execution time limit]
    4 seconds (py3)

  • [input] array.array.integer matrix
    Guaranteed constraints: 1 ≤ matrix.length ≤ 100, 1 ≤ matrix[i].length ≤ 100, 0 ≤ matrix[i][j] ≤ 9.

  • [output] integer
    The number of different 2 × 2 squares in matrix.

My Solution

def differentSquares(matrix):
    squares = set([((matrix[row][column], matrix[row][column + 1]),
                    (matrix[row + 1][column], matrix[row + 1][column + 1]))
                  for column in range(len(matrix[0]) - 1) for row in range(len(matrix) - 1)])
    return len(squares)