Check if There Is a Valid Parentheses String Path - Problem

A parentheses string is a non-empty string consisting only of '(' and ')'. It is valid if any of the following conditions is true:

  • It is ()
  • It can be written as AB (A concatenated with B), where A and B are valid parentheses strings
  • It can be written as (A), where A is a valid parentheses string

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:

  • The path starts from the upper left cell (0, 0)
  • The path ends at the bottom-right cell (m - 1, n - 1)
  • The path only ever moves down or right
  • The resulting parentheses string formed by the path is valid

Return true if there exists a valid parentheses string path in the grid. Otherwise, return false.

Input & Output

Example 1 — Valid Path Exists
$ Input: grid = [["(",")"],["(",")"]]
Output: false
💡 Note: There are two possible paths: (0,0)→(0,1)→(1,1) gives "())" which is invalid (balance becomes -1), and (0,0)→(1,0)→(1,1) gives "(()" which is invalid (final balance is 1, not 0). No valid path exists.
Example 2 — No Valid Path
$ Input: grid = [[")","("],["(",")"]]
Output: false
💡 Note: All paths start with ")" which immediately makes the balance negative, so no valid parentheses string can be formed.
Example 3 — Larger Grid
$ Input: grid = [["(","(",")"],[")",")","("],["(",")",")"]]
Output: true
💡 Note: There exists at least one path that forms a valid parentheses string by carefully choosing down/right moves.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 100
  • grid[i][j] is either '(' or ')'

Visualization

Tap to expand
Valid Parentheses String Path INPUT 2x2 Parentheses Grid ( ) ( ) (0,0) (0,1) (1,0) (1,1) Start End Possible Paths: Path 1: ( --> ) --> ) = ()) Path 2: ( --> ( --> ) = (() Input Parameters: grid = [["(",")"], ["(",")"]] ALGORITHM STEPS 1 DP with Balance Track open paren count at each cell position 2 State: dp[i][j][k] k = balance (open - close) Can reach (i,j) with k? 3 Transitions '(' adds +1 to balance ')' subtracts 1 from bal 4 Check Final State Valid if dp[m-1][n-1][0] is true (balance = 0) Balance States at (1,1): Path "(()": bal=1 [Invalid] Path "())": bal=-1 [Pruned] Wait - check paths! ( --> ( --> ) --> ) = (()) FINAL RESULT Valid Path Found! ( ) ( ) "(())" Valid Parentheses! Balance Check: ( +1 ( +1 ) 0 ) -1 Wait: (()->1, ()->0 [OK] Output: true Key Insight: Use 3D DP where dp[i][j][k] represents whether we can reach cell (i,j) with balance k (open - close parens). Prune states where balance goes negative (invalid). Path length is fixed at (m+n-1), so if odd, return false early. Time: O(m*n*(m+n)), Space: O(m*n*(m+n)). Valid path exists if dp[m-1][n-1][0] is reachable. TutorialsPoint - Check if There Is a Valid Parentheses String Path | Optimal Solution
Asked in
Google 45 Amazon 38 Microsoft 32 Facebook 28
23.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen