Problem · Array

Validate 3x3 Digit Windows

Learn this problem
EasyMatroid logoMatroidINTERNOA

Problem statement

You are given numbers, a 3 x n matrix containing only digits from 1 through 9.

Consider a 3 x 3 window that slides from left to right through numbers. It has n - 2 positions.

For every position, determine whether the nine cells contain all numbers from 1 through 9, inclusive. Return a boolean array of length n - 2 in left-to-right window order. Its i-th element is true exactly when the i-th window contains all nine numbers, and false otherwise.

Because each window contains exactly nine cells, a valid window contains each digit from 1 through 9 exactly once.

A solution with time complexity no worse than O(numbers[0].length^3) fits within the execution time limit.

Function

solution(numbers: int[][]) → boolean[]

Examples

Example 1

numbers = [[1, 2, 3, 2, 5, 7], [4, 5, 6, 1, 7, 6], [7, 8, 9, 4, 8, 3]]return = [true, false, true, false]

The first window contains every digit from 1 through 9, so its result is true. The second is missing 7 and contains 2 twice, so it is false. The third again contains all nine digits, so it is true. The final window is missing 9 and contains 7 twice, so it is false.

Constraints

  • numbers.length == 3
  • Every row of numbers has the same length n.
  • n >= 3
  • 1 <= numbers[row][col] <= 9

More Matroid problems

drafts saved locally
public boolean[] solution(int[][] numbers) {
  // Write your code here.
}
numbers[[1, 2, 3, 2, 5, 7], [4, 5, 6, 1, 7, 6], [7, 8, 9, 4, 8, 3]]
expected[true, false, true, false]
checking account