返回介绍

solution / 2200-2299 / 2267.Check if There Is a Valid Parentheses String Path / README_EN

发布于 2024-06-17 01:03:08 字数 6415 浏览 0 评论 0 收藏 0

2267. Check if There Is a Valid Parentheses String Path

中文文档

Description

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.

 

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 ')'.

Solutions

Solution 1

class Solution:
  def hasValidPath(self, grid: List[List[str]]) -> bool:
    @cache
    def dfs(i, j, t):
      if grid[i][j] == '(':
        t += 1
      else:
        t -= 1
      if t < 0:
        return False
      if i == m - 1 and j == n - 1:
        return t == 0
      for x, y in [(i + 1, j), (i, j + 1)]:
        if x < m and y < n and dfs(x, y, t):
          return True
      return False

    m, n = len(grid), len(grid[0])
    return dfs(0, 0, 0)
class Solution {
  private boolean[][][] vis;
  private char[][] grid;
  private int m;
  private int n;

  public boolean hasValidPath(char[][] grid) {
    m = grid.length;
    n = grid[0].length;
    this.grid = grid;
    vis = new boolean[m][n][m + n];
    return dfs(0, 0, 0);
  }

  private boolean dfs(int i, int j, int t) {
    if (vis[i][j][t]) {
      return false;
    }
    vis[i][j][t] = true;
    t += grid[i][j] == '(' ? 1 : -1;
    if (t < 0) {
      return false;
    }
    if (i == m - 1 && j == n - 1) {
      return t == 0;
    }
    int[] dirs = {0, 1, 0};
    for (int k = 0; k < 2; ++k) {
      int x = i + dirs[k], y = j + dirs[k + 1];
      if (x < m && y < n && dfs(x, y, t)) {
        return true;
      }
    }
    return false;
  }
}
bool vis[100][100][200];
int dirs[3] = {1, 0, 1};

class Solution {
public:
  bool hasValidPath(vector<vector<char>>& grid) {
    memset(vis, 0, sizeof(vis));
    return dfs(0, 0, 0, grid);
  }

  bool dfs(int i, int j, int t, vector<vector<char>>& grid) {
    if (vis[i][j][t]) return false;
    vis[i][j][t] = true;
    t += grid[i][j] == '(' ? 1 : -1;
    if (t < 0) return false;
    int m = grid.size(), n = grid[0].size();
    if (i == m - 1 && j == n - 1) return t == 0;
    for (int k = 0; k < 2; ++k) {
      int x = i + dirs[k], y = j + dirs[k + 1];
      if (x < m && y < n && dfs(x, y, t, grid)) return true;
    }
    return false;
  }
};
func hasValidPath(grid [][]byte) bool {
  m, n := len(grid), len(grid[0])
  vis := make([][][]bool, m)
  for i := range vis {
    vis[i] = make([][]bool, n)
    for j := range vis[i] {
      vis[i][j] = make([]bool, m+n)
    }
  }
  var dfs func(int, int, int) bool
  dfs = func(i, j, t int) bool {
    if vis[i][j][t] {
      return false
    }
    vis[i][j][t] = true
    if grid[i][j] == '(' {
      t += 1
    } else {
      t -= 1
    }
    if t < 0 {
      return false
    }
    if i == m-1 && j == n-1 {
      return t == 0
    }
    dirs := []int{1, 0, 1}
    for k := 0; k < 2; k++ {
      x, y := i+dirs[k], j+dirs[k+1]
      if x < m && y < n && dfs(x, y, t) {
        return true
      }
    }
    return false
  }
  return dfs(0, 0, 0)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文