Problem · Array

Validate 3x3 Digit Windows

Learn this problem
EasyTiktokINTERNOA
See Tiktok hiring insights

Problem statement

You are given a matrix numbers consisting of 3 rows and n columns, with digits from 1 to 9. Consider a sliding window of size 3 x 3 that moves from left to right through the matrix numbers. The sliding window has n - 2 positions when sliding through the initial matrix.

Your task is to find whether or not each sliding window position contains all the numbers from 1 to 9, inclusively.

Return an array of length n - 2, where the i-th element is true if the i-th state of the sliding window contains all the numbers from 1 to 9, and false otherwise.

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

You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(numbers[0].length^3) will fit within the execution time limit.

Function

validateDigitWindows(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, covering columns 0 through 2, contains every digit from 1 to 9, so its result is true.

The second window is missing 7 and contains 2 twice, so its result is false. The third window again contains every digit from 1 to 9, so its result is true. The final window is missing 9 and contains 7 twice, so its result is false.

Example 2

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

There is only one window, and its nine cells contain each digit from 1 through 9 exactly once.

Example 3

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

The only window contains 8 twice and does not contain 9, so it is not valid.

Constraints

  • numbers.length == 3
  • numbers[i].length == numbers[0].length
  • numbers[0].length >= 3
  • 1 <= numbers[i][j] <= 9

More Tiktok problems

drafts saved locally
public boolean[] validateDigitWindows(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