Check if There Is a Valid Parentheses String Path - Practice Coding | SlaveCode
0
0123456789
0
0123456789
:
0
0123456789
0
0123456789
2267. Check if There Is a Valid Parentheses String Path
Hard
50 Points
Array
Dynamic Programming
Matrix
A parentheses string is a non-empty string consisting only of '(' and ')'. It is valid if any of the following conditions is true:
You are given an m x n matrix of parentheses grid. A valid parentheses string path in the grid is a path satisfying all of the following conditions:
Return true if there exists a valid parentheses string path in the grid. Otherwise, return false.
Examples
Example 1
Input: grid = [["(","(","("],[")","(",")"],["(","(",")"],["(","(",")"]]
Output: true
Explanation: The above diagram shows two possible paths that form valid parentheses strings.
The first path shown results in the valid parentheses string "()(())".
The second path shown results in the valid parentheses string "((()))".
Note that there may be other valid parentheses string paths.
Example 2
Input: grid = [[")",")"],["(","("]]
Output: false
Explanation: The two possible paths form the parentheses strings "))(" and ")((". Since neither of them are valid parentheses strings, we return false.
Constraints
m == grid.length
n == grid[i].length
1 <= m, n <= 100
grid[i][j] is either '(' or ')'.
2267. Check if There Is a Valid Parentheses String Path
Hard
50 Points
Array
Dynamic Programming
Matrix
A parentheses string is a non-empty string consisting only of '(' and ')'. It is valid if any of the following conditions is true:
You are given an m x n matrix of parentheses grid. A valid parentheses string path in the grid is a path satisfying all of the following conditions:
Return true if there exists a valid parentheses string path in the grid. Otherwise, return false.
Examples
Example 1
Input: grid = [["(","(","("],[")","(",")"],["(","(",")"],["(","(",")"]]
Output: true
Explanation: The above diagram shows two possible paths that form valid parentheses strings.
The first path shown results in the valid parentheses string "()(())".
The second path shown results in the valid parentheses string "((()))".
Note that there may be other valid parentheses string paths.
Example 2
Input: grid = [[")",")"],["(","("]]
Output: false
Explanation: The two possible paths form the parentheses strings "))(" and ")((". Since neither of them are valid parentheses strings, we return false.