Different Squares

Description


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 (js)

  • [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.

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
    console.log("This prints to the console when you Run Tests");
    return "Hello, " + name;
}

Solution


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function differentSquares(matrix) {
    var d = [];
    for(var i = 1;  i < matrix.length; i++) {
        for (var j = 1; j < matrix[i].length; j++) {
            if(not(i,j)){
                d.push([
                    [
                        matrix[i-1][j-1],
                        matrix[i-1][j]
                    ],[
                        matrix[i][j-1],
                        matrix[i][j]
                    ]
                ]);
            }
        }
    }
    
    function not(i, j) {
        return d.every(c=>{
           return c[0][0] !== matrix[i-1][j-1] ||
               c[0][1] !== matrix[i-1][j] || 
               c[1][0] !== matrix[i][j-1] ||
               c[1][1] !== matrix[i][j]
        });
    }
    return d.length;
}