As reported: Google L4 phone screen (January 2025): a grid of 0s and 1s, moves down or right from the top-left corner to the target cell; return
trueif some path passes as many 1s as 0s. A DFS over all paths was given. Follow-up (not solved): moves in all four directions.
You are given an m × n binary matrix grid. You may move from (r, c) to (r + 1, c) or (r, c + 1). Return true if there is a path from (0, 0) to (m − 1, n − 1) that visits an equal number of 0s and 1s, otherwise false.
This is LeetCode 2510 — Check if There is a Path With Equal Number of 0's And 1's.
Example
0 1 0 0
0 1 0 0
1 0 1 0 -> true
1 1 0
0 0 1
1 0 0 -> false
Constraints: 2 ≤ m, n ≤ 100; grid[i][j] is 0 or 1.
Code runner format: LeetCode's format: one line with grid as a JSON array of arrays. Implement isThereAPath(grid).
Follow-ups
- Moves are allowed in all four directions (a path still visits each cell at most once).