2267. Check if There Is a Valid Parentheses String Path
</> Solution
/**
* Problem: LeetCode 2267 - Check if There Is a Valid Parentheses String Path
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public boolean hasValidPath(char[][] grid) {
int m = grid.length, n = grid[0].length;
if (((m + n - 1) & 1) == 1) return false;
if (grid[0][0] == ')' || grid[m - 1][n - 1] == '(') return false;
int maxBal = (m + n - 1) / 2;
boolean[][][] dp = new boolean[m][n][maxBal + 2];
dp[0][0][1] = true;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 && j == 0) continue;
int delta = grid[i][j] == '(' ? 1 : -1;
for (int b = 0; b <= maxBal; b++) {
int prev = b - delta;
if (prev < 0 || prev > maxBal) continue;
boolean reachable = false;
if (i > 0 && dp[i - 1][j][prev]) reachable = true;
if (j > 0 && dp[i][j - 1][prev]) reachable = true;
if (reachable) dp[i][j][b] = true;
}
}
}
return dp[m - 1][n - 1][0];
}
}
"""
Problem: LeetCode 2267 - Check if There Is a Valid Parentheses String Path
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
class Solution:
def hasValidPath(self, grid: list[list[str]]) -> bool:
m, n = len(grid), len(grid[0])
total_len = m + n - 1
# Path length must be even to form a valid balanced parentheses string
if total_len % 2 == 1:
return False
if grid[0][0] == ')' or grid[m - 1][n - 1] == '(':
return False
max_bal = total_len // 2
# dp[i][j][b] tracks if open parenthesis balance b is reachable at cell (i, j)
dp = [[[False] * (max_bal + 2) for _ in range(n)] for _ in range(m)]
dp[0][0][1] = True
for i in range(m):
for j in range(n):
if i == 0 and j == 0:
continue
delta = 1 if grid[i][j] == '(' else -1
for b in range(max_bal + 1):
prev = b - delta
if 0 <= prev <= max_bal:
reachable = False
if i > 0 and dp[i - 1][j][prev]:
reachable = True
if j > 0 and dp[i][j - 1][prev]:
reachable = True
if reachable:
dp[i][j][b] = True
return dp[m - 1][n - 1][0]
/**
* Problem: LeetCode 2267 - Check if There Is a Valid Parentheses String Path
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
/**
* Problem: LeetCode 2267 - Check if There Is a Valid Parentheses String Path
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
bool hasValidPath(std::vector<std::vector<char>>& grid) {
int m = grid.size(), n = grid[0].size();
int totalLen = m + n - 1;
// Path length must be even to form a valid balanced parentheses string
if ((totalLen & 1) == 1) return false;
if (grid[0][0] == ')' || grid[m - 1][n - 1] == '(') return false;
int maxBal = totalLen / 2;
// dp[i][j][b] represents if open parenthesis balance 'b' is reachable at cell (i, j)
std::vector<std::vector<std::vector<bool>>> dp(
m, std::vector<std::vector<bool>>(n, std::vector<bool>(maxBal + 2, false))
);
dp[0][0][1] = true;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i == 0 && j == 0) continue;
int delta = (grid[i][j] == '(') ? 1 : -1;
for (int b = 0; b <= maxBal; ++b) {
int prev = b - delta;
if (prev < 0 || prev > maxBal) continue;
bool reachable = false;
if (i > 0 && dp[i - 1][j][prev]) reachable = true;
if (j > 0 && dp[i][j - 1][prev]) reachable = true;
if (reachable) dp[i][j][b] = true;
}
}
}
return dp[m - 1][n - 1][0];
}
};
/**
* Problem: LeetCode 2267 - Check if There Is a Valid Parentheses String Path
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
var hasValidPath = function(grid) {
const m = grid.length, n = grid[0].length;
const totalLen = m + n - 1;
// Path length must be even to form a valid balanced parentheses string
if ((totalLen & 1) === 1) return false;
if (grid[0][0] === ')' || grid[m - 1][n - 1] === '(') return false;
const maxBal = Math.floor(totalLen / 2);
// dp[i][j][b] represents whether open parenthesis balance 'b' is reachable at cell (i, j)
const dp = Array.from({ length: m }, () =>
Array.from({ length: n }, () => new Uint8Array(maxBal + 2))
);
dp[0][0][1] = 1;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (i === 0 && j === 0) continue;
const delta = grid[i][j] === '(' ? 1 : -1;
for (let b = 0; b <= maxBal; b++) {
const prev = b - delta;
if (prev < 0 || prev > maxBal) continue;
let reachable = false;
if (i > 0 && dp[i - 1][j][prev]) reachable = true;
if (j > 0 && dp[i][j - 1][prev]) reachable = true;
if (reachable) dp[i][j][b] = 1;
}
}
}
return dp[m - 1][n - 1][0] === 1;
};