返回介绍

solution / 2100-2199 / 2116.Check if a Parentheses String Can Be Valid / README_EN

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

2116. Check if a Parentheses String Can Be Valid

中文文档

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 a parentheses string s and a string locked, both of length n. locked is a binary string consisting only of '0's and '1's. For each index i of locked,

  • If locked[i] is '1', you cannot change s[i].
  • But if locked[i] is '0', you can change s[i] to either '(' or ')'.

Return true _if you can make s a valid parentheses string_. Otherwise, return false.

 

Example 1:

Input: s = "))()))", locked = "010100"
Output: true
Explanation: locked[1] == '1' and locked[3] == '1', so we cannot change s[1] or s[3].
We change s[0] and s[4] to '(' while leaving s[2] and s[5] unchanged to make s valid.

Example 2:

Input: s = "()()", locked = "0000"
Output: true
Explanation: We do not need to make any changes because s is already valid.

Example 3:

Input: s = ")", locked = "0"
Output: false
Explanation: locked permits us to change s[0]. 
Changing s[0] to either '(' or ')' will not make s valid.

 

Constraints:

  • n == s.length == locked.length
  • 1 <= n <= 105
  • s[i] is either '(' or ')'.
  • locked[i] is either '0' or '1'.

Solutions

Solution 1

class Solution:
  def canBeValid(self, s: str, locked: str) -> bool:
    n = len(s)
    if n & 1:
      return False
    x = 0
    for i in range(n):
      if s[i] == '(' or locked[i] == '0':
        x += 1
      elif x:
        x -= 1
      else:
        return False
    x = 0
    for i in range(n - 1, -1, -1):
      if s[i] == ')' or locked[i] == '0':
        x += 1
      elif x:
        x -= 1
      else:
        return False
    return True
class Solution {
  public boolean canBeValid(String s, String locked) {
    int n = s.length();
    if (n % 2 == 1) {
      return false;
    }
    int x = 0;
    for (int i = 0; i < n; ++i) {
      if (s.charAt(i) == '(' || locked.charAt(i) == '0') {
        ++x;
      } else if (x > 0) {
        --x;
      } else {
        return false;
      }
    }
    x = 0;
    for (int i = n - 1; i >= 0; --i) {
      if (s.charAt(i) == ')' || locked.charAt(i) == '0') {
        ++x;
      } else if (x > 0) {
        --x;
      } else {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool canBeValid(string s, string locked) {
    int n = s.size();
    if (n & 1) {
      return false;
    }
    int x = 0;
    for (int i = 0; i < n; ++i) {
      if (s[i] == '(' || locked[i] == '0') {
        ++x;
      } else if (x) {
        --x;
      } else {
        return false;
      }
    }
    x = 0;
    for (int i = n - 1; i >= 0; --i) {
      if (s[i] == ')' || locked[i] == '0') {
        ++x;
      } else if (x) {
        --x;
      } else {
        return false;
      }
    }
    return true;
  }
};
func canBeValid(s string, locked string) bool {
  n := len(s)
  if n%2 == 1 {
    return false
  }
  x := 0
  for i := range s {
    if s[i] == '(' || locked[i] == '0' {
      x++
    } else if x > 0 {
      x--
    } else {
      return false
    }
  }
  x = 0
  for i := n - 1; i >= 0; i-- {
    if s[i] == ')' || locked[i] == '0' {
      x++
    } else if x > 0 {
      x--
    } else {
      return false
    }
  }
  return true
}

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

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

发布评论

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